各位大侠帮帮小弟看看这代码为什么不能发送邮件!感激不尽!
string mailSubject = "会员注册确认函";
string mailBody = "您好!您的注册已经成功,您的注册名为:'" + U_name + "'密码为:'" + U_password + "'"; ;
string mailFrom = ConfigurationManager.AppSettings["SendMail"];
if (MySendMail(mailTo, mailSubject, mailBody, mailFrom))
{
Response.Write("<script>alert('发送成功!')</script>"); //发送成功,进行相应处理
}
else
{
Response.Write("<script>alert('发送失败!')</script>"); //发送失败,进行相应处理
return;
}
public bool MySendMail(string mailto, string mailsubject, string mailbody, string mailFrom)
{
try
{
//邮件发送人地址
System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress(mailFrom);
//如test@163.com,初步测试,用test@sina.com不行,用163的邮件服务器,就必须用163邮箱的用户名
//收件人地址
System.Net.Mail.MailAddress to = new System.Net.Mail.MailAddress(mailto);//如test@tom.com
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(from, to);
mail.Subject = mailsubject;
mail.Body = mailbody;
//以下设置服务器
System.Net.Mail.SmtpClient mySmth = new System.Net.Mail.SmtpClient();
mySmth.Send(mail);
mail.Dispose();
}
catch
{
return false;
}
return true;
}
------解决方案--------------------好像没什么错,我的代码如下:可以正常发送,你看一下吧
SmtpClient client = new SmtpClient();
client.Host = txtSMTP.Text.Trim();
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(txtSendName.Text.Trim(), txtPassword.Text.Trim());
client.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
for (int i = 0; i < listBox1.Items.Count; i++)
{
MailMessage message = new MailMessage(txtSendName.Text.Trim(), listBox1.Items[i].ToString());
message.Subject = "测试";
message.Body = "用自己写的软件发邮件!";
message.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
message.IsBodyHtml = true;
//添加附件
for (int j = 0; j < listBox2.Items.Count; j++)
{
Attachment data = new Attachment(listBox2.Items[j].ToString(), System.Net.Mime.MediaTypeNames.Application.Octet);
message.Attachments.Add(data);
}
client.Send(message);
}
MessageBox.Show("Email successfully send.");
}
catch (Exception ex)
{
MessageBox.Show("Send Email Failed." + ex.ToString());
}
------解决方案--------------------我原来也做过,用163的发163的不行,用GMAIL发给别的邮箱就可以
------解决方案--------------------C# code
client.Credentials = new System.Net.NetworkCredential(txtSendName.Text.Trim(), txtPassword.Text.Trim());