当前位置: 编程技术>.net/c#/asp.net
asp.net SmtpClient发送邮件
来源: 互联网 发布时间:2014-08-30
本文导语: 例1,163邮箱发送邮件 HOST:smtp.163.com 代码: 代码示例: public static string CreateTimeoutTestMessage(string server) { string Success = "发送成功"; try { string _to = "1035092449@qq.com"; string _from = "young-20@163.com"; ...
例1,163邮箱发送邮件
HOST:smtp.163.com
代码:
代码示例:
public static string CreateTimeoutTestMessage(string server)
{
string Success = "发送成功";
try
{
string _to = "1035092449@qq.com";
string _from = "young-20@163.com";
string _subject = "Using the new SMTP client.";
string _body = @"Using this new feature, you can send an e-mail message from an application very easily.";
MailMessage message = new MailMessage();
message.From = new MailAddress(_from);
//可以利用MailMessage.To.Add方法增加要发送的邮件地址
message .To .Add (new MailAddress ("652105072@qq.com"));
message.To.Add(new MailAddress(_to));
message.Subject = _subject;
message.Body = _body;
//添加附件
Attachment a = new Attachment(@"C:/Users/Administrator/Desktop/smtpclient.rar");
message.Attachments.Add(a);
//设置邮箱的地址或IP
SmtpClient client = new SmtpClient(server);
//设置邮箱端口,pop3端口:110, smtp端口是:25
client.Port = 25;
//设置超时时间
client.Timeout = 9999;
//要输入邮箱用户名与密码
client.Credentials = new NetworkCredential("young-20@163.com", "******");
client.Send(message);
}
catch (Exception ex)
{
Success = ex.ToString();
}
return Success;
}
{
string Success = "发送成功";
try
{
string _to = "1035092449@qq.com";
string _from = "young-20@163.com";
string _subject = "Using the new SMTP client.";
string _body = @"Using this new feature, you can send an e-mail message from an application very easily.";
MailMessage message = new MailMessage();
message.From = new MailAddress(_from);
//可以利用MailMessage.To.Add方法增加要发送的邮件地址
message .To .Add (new MailAddress ("652105072@qq.com"));
message.To.Add(new MailAddress(_to));
message.Subject = _subject;
message.Body = _body;
//添加附件
Attachment a = new Attachment(@"C:/Users/Administrator/Desktop/smtpclient.rar");
message.Attachments.Add(a);
//设置邮箱的地址或IP
SmtpClient client = new SmtpClient(server);
//设置邮箱端口,pop3端口:110, smtp端口是:25
client.Port = 25;
//设置超时时间
client.Timeout = 9999;
//要输入邮箱用户名与密码
client.Credentials = new NetworkCredential("young-20@163.com", "******");
client.Send(message);
}
catch (Exception ex)
{
Success = ex.ToString();
}
return Success;
}
例2,QQ邮箱邮箱发送邮件
QQ邮箱默认的SMTP服务是关闭的,要自己去开通。
HOST:smtp.qq.com
代码:
代码示例:
try
{
SmtpClient client = new SmtpClient();
client.Host = "smtp.qq.com";
MailMessage mm = new MailMessage();
client.Port = 25;
mm.From = new MailAddress("652105072@qq.com");
mm.To.Add(new MailAddress("1035092449@qq.com"));
mm.Subject = "Hello~!";
mm.Body = "HIthere.here is a post ";
mm.IsBodyHtml = false;
mm.Priority = MailPriority.High;
client.Credentials = new NetworkCredential("652105072@qq.com", "******");
client .Send (mm);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
{
SmtpClient client = new SmtpClient();
client.Host = "smtp.qq.com";
MailMessage mm = new MailMessage();
client.Port = 25;
mm.From = new MailAddress("652105072@qq.com");
mm.To.Add(new MailAddress("1035092449@qq.com"));
mm.Subject = "Hello~!";
mm.Body = "HIthere.here is a post ";
mm.IsBodyHtml = false;
mm.Priority = MailPriority.High;
client.Credentials = new NetworkCredential("652105072@qq.com", "******");
client .Send (mm);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}