为什么发送邮件时会超时?

duling509 2008-12-08 02:37:06

public static string Send() {
string result = "";
try {
MailMessage mail = new MailMessage("duling509@163.com", "duling803@163.com", "主题", "内容");
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.163.com";//如果使用163发送
smtp.Credentials = new System.Net.NetworkCredential("duling509@163.com", "7889698");//邮箱账号与密码
smtp.Send(mail);
}
catch (Exception ex) {
result = ex.Message;
}
return result;
}


返回的result都是操作已超时
...全文
420 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
l32782908 2008-12-09
  • 打赏
  • 举报
回复
关注中
mykelly6 2008-12-09
  • 打赏
  • 举报
回复
应该有很多完整的例子吧,搜一下把用户名密码改下就可以了,不过我以前用别人的java代码发的确报错~
IFocusYou 2008-12-09
  • 打赏
  • 举报
回复

namespace ASB.Test.Service.MailService
{
public class MailSender
{
private static string SMTPServer = AppSettings["SMTPServer"];
private static int SMTPPort = int.Parse(AppSettings["SMTPPort"]);
private static string SMTPFrom = AppSettings["From"];
private static string SMTPFromPwd = AppSettings["FromPwd"];
private static string SMTPFromDisplay = AppSettings["FromDisplay"];
private static System.Collections.Specialized.NameValueCollection AppSettings { get { return System.Configuration.ConfigurationManager.AppSettings; } }


public static bool SendEmail(string from, string fromDisplay, string to, string toDisplay, string cc, string ccDisplay, string subject, string body, List<MailAttachment> attachments)
{
bool isSuccess = false;

try
{
SmtpClient client = new SmtpClient(SMTPServer);
client.Credentials = new System.Net.NetworkCredential(SMTPFrom, SMTPFromPwd);

//client.EnableSsl = true;
MailAddress fromAddr = new MailAddress(from, fromDisplay);

// Set destinations for the e-mail message.
MailAddress toAddr = new MailAddress(to, toDisplay);

// Specify the message content.
MailMessage message = new MailMessage(fromAddr, toAddr);

if (cc != string.Empty)
{
MailAddress ccAddr = new MailAddress(cc, ccDisplay);
message.CC.Add(ccAddr);
}

message.IsBodyHtml = true;
message.Body = body;
message.BodyEncoding = System.Text.Encoding.UTF8;

message.Subject = subject;
message.SubjectEncoding = System.Text.Encoding.UTF8;

if (attachments != null)
{
foreach (MailAttachment mailAttach in attachments)
{
if (mailAttach.AttachmentData != null)
{
Attachment attach = new Attachment(mailAttach.AttachmentData, mailAttach.AttachmentName);

message.Attachments.Add(attach);
}
}
}
client.DeliveryMethod = SmtpDeliveryMethod.Network;

client.Send(message);

message.Dispose();

isSuccess = true;
return isSuccess;
}
catch (Exception ex)
{
try
{
isSuccess = false;
}
catch
{
isSuccess = false;
}
return isSuccess;
}
}


public static bool SendEmail(string to, string toDisplay, string cc, string ccDisplay, string subject, string body)
{
return SendEmail(SMTPFrom, SMTPFromDisplay, to, toDisplay, cc, ccDisplay, subject, body,null);
}

public static bool SendEmail(string to, string toDisplay, string cc, string ccDisplay, string subject, string body, List<MailAttachment> attachments)
{
return SendEmail(SMTPFrom, SMTPFromDisplay, to, toDisplay, cc, ccDisplay, subject, body, attachments);
}
}
}
benyouyong 2008-12-09
  • 打赏
  • 举报
回复
smtp.Port=...
这个也要设置哦。
niitnanfeng 2008-12-08
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20081208/14/7cc7c75c-e1ee-443b-b210-8bb899ed0cc9.html
看下那个,N多的方案。肯定有适合你的。
netdevgirl 2008-12-08
  • 打赏
  • 举报
回复
163邮箱发送邮件时是要求用户验证的,所以发不成功
楼上的好幽默啊
benyouyong 2008-12-08
  • 打赏
  • 举报
回复
因为报异常了吧。
邮箱名和密码就不要告诉我了。
我不做坏事。

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧