首页 新闻 赞助 找找看

C# smtp.qq.com 发送邮件失败

0
悬赏园豆:5 [已关闭问题] 关闭于 2017-04-13 17:49

为什么会报错?以下是我的代码:

public bool SendEmail(string subject, string body, string to, string cc, string attachment, string mailServerAddress, string port,string mailAddress, string mailPassword)
{
try
{
string[] arrTo = to.Split(';');
string[] arrCc = cc.Split(';');
string[] arrAttachment = attachment.Split(';');


MailMessage msg = new MailMessage();
msg.From = new MailAddress(mailAddress);//发件人邮箱

foreach (string mail in arrTo)//收件人地址
{
if (mail.Trim().Length > 0)
{
msg.To.Add(new MailAddress(mail));
}
}
foreach (string mail in arrCc)//抄送人地址
{
if (mail.Trim().Length > 0)
{
msg.CC.Add(new MailAddress(mail));
}
}
foreach (string attach in arrAttachment)//附件
{
if (attach.Trim().Length > 0)
{
msg.Attachments.Add(new Attachment(attach));
}
}

msg.Subject = subject;//邮件标题
msg.SubjectEncoding = Encoding.UTF8; //主题编码

msg.Body = body;//邮件内容
msg.BodyEncoding = Encoding.UTF8;
//msg.IsBodyHtml = false; //设置为HTML格式
//msg.Priority = MailPriority.High; //优先级

SmtpClient client = new SmtpClient();
client.Host = mailServerAddress;
client.Port = int.Parse(port);
//client.UseDefaultCredentials = true;
client.EnableSsl = true;
client.Credentials = new NetworkCredential("524082999", mailPassword);

//client.DeliveryMethod = SmtpDeliveryMethod.Network;
//msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
client.Send(msg);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
return true;

}

青鋒的主页 青鋒 | 初学一级 | 园豆:5
提问于:2017-04-13 17:48
< >
分享
所有回答(1)
0

调试可以么

codingHeart | 园豆:1511 (小虾三级) | 2017-04-13 17:49

没用,捕捉的异常就是那句提示

支持(0) 反对(0) 青鋒 | 园豆:5 (初学一级) | 2017-04-13 17:50

@青鋒: 哪一句出现的异常呢

支持(0) 反对(0) codingHeart | 园豆:1511 (小虾三级) | 2017-04-13 17:52

@codingHeart: client.Send(msg);

支持(0) 反对(0) 青鋒 | 园豆:5 (初学一级) | 2017-04-13 17:56

@codingHeart: 我的QQ邮箱是已经开启了POP3/SMTP服务的,但试了网上好多示例都不行,都提示那个报错

支持(0) 反对(0) 青鋒 | 园豆:5 (初学一级) | 2017-04-13 17:57

@codingHeart: 还有捕捉到“无法远程连接至远端服务器”

支持(0) 反对(0) 青鋒 | 园豆:5 (初学一级) | 2017-04-13 17:59

@青鋒: 我记得如果频繁用它的邮件服务器发邮件会有限制吧,你们公司自己的邮件服务器呢

支持(0) 反对(0) codingHeart | 园豆:1511 (小虾三级) | 2017-04-13 18:04

@codingHeart: 这是开发给客户用的程序,没办法用公司的

支持(0) 反对(0) 青鋒 | 园豆:5 (初学一级) | 2017-04-13 18:06
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册