发送邮件的几种方法
2006-09-03 00:35
441 查看
使用System.Web.Mail发送邮件:
public void SendMail()
{
MailMessage mail1 = new MailMessage();
mail1.Body="body here"; //邮件的正文
mail1.From="xxx@your.com"; //发信人的地址
mail1.To="yyy@their.com"; //收信人的地址
mail1.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",1); //要求smtp认证
mail1.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername","xxx"); //smtp认证的用户
mail1.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword","********"); //smtp认证的密码
SmtpMail.SmtpServer="mail.your.com"; //smtp服务器
SmtpMail.Send(mail1); //发送邮件
}
使用System.Net.Mail发送邮件:
发现用这种方式发邮件,如果发邮件的计算机开启了杀毒软件的邮件监控,会有错误提示,但能够发信成功。
1)第一种方式:先配置web.config文件
<system.net>
<mailSettings>
<smtp from="your@your.com">
<network host="smtp.your.com" port="25" userName="your" password="yourpass" />
</smtp>
</mailSettings>
</system.net>
然后通下面的程序发信:
private void SendMail()
{
SmtpClient smtp = new SmtpClient();
MailMessage message = new MailMessage();
message.To.Add("to@yourmail.com"); //收信人地址
message.SubjectEncoding = System.Text.Encoding.UTF8; //主题文字编码方式
message.BodyEncoding = System.Text.Encoding.UTF8; //内容文字编码方式
message.Subject = "A test for sending mail"; //主题
message.Body = "Thanks for your sending mail./n/n"; //内容
smtp.Send(message); //发信
message.Dispose();
}
2)第二种方式是不配置web.config文件,直接通过下面的程序发信:
private void SendMail()
{
SmtpClient smtp = new SmtpClient("mail.your.com", 25);
smtp.Credentials = new System.Net.NetworkCredential("from@your.com", "yourpass"); //提供发信smtp认证信息
MailAddress from = new MailAddress("from@your.com", "tiger", System.Text.Encoding.UTF8); //发信人信息
MailAddress to = new MailAddress("to@their.com"); //收信人信息
MailMessage message = new MailMessage(from, to);
message.Subject = "这里是主题";
message.Body = "正文内容";
message.SubjectEncoding = System.Text.Encoding.UTF8;
message.BodyEncoding = System.Text.Encoding.UTF8;
smtp.Send(message);
message.Dispose();
}
public void SendMail()
{
MailMessage mail1 = new MailMessage();
mail1.Body="body here"; //邮件的正文
mail1.From="xxx@your.com"; //发信人的地址
mail1.To="yyy@their.com"; //收信人的地址
mail1.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",1); //要求smtp认证
mail1.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername","xxx"); //smtp认证的用户
mail1.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword","********"); //smtp认证的密码
SmtpMail.SmtpServer="mail.your.com"; //smtp服务器
SmtpMail.Send(mail1); //发送邮件
}
使用System.Net.Mail发送邮件:
发现用这种方式发邮件,如果发邮件的计算机开启了杀毒软件的邮件监控,会有错误提示,但能够发信成功。
1)第一种方式:先配置web.config文件
<system.net>
<mailSettings>
<smtp from="your@your.com">
<network host="smtp.your.com" port="25" userName="your" password="yourpass" />
</smtp>
</mailSettings>
</system.net>
然后通下面的程序发信:
private void SendMail()
{
SmtpClient smtp = new SmtpClient();
MailMessage message = new MailMessage();
message.To.Add("to@yourmail.com"); //收信人地址
message.SubjectEncoding = System.Text.Encoding.UTF8; //主题文字编码方式
message.BodyEncoding = System.Text.Encoding.UTF8; //内容文字编码方式
message.Subject = "A test for sending mail"; //主题
message.Body = "Thanks for your sending mail./n/n"; //内容
smtp.Send(message); //发信
message.Dispose();
}
2)第二种方式是不配置web.config文件,直接通过下面的程序发信:
private void SendMail()
{
SmtpClient smtp = new SmtpClient("mail.your.com", 25);
smtp.Credentials = new System.Net.NetworkCredential("from@your.com", "yourpass"); //提供发信smtp认证信息
MailAddress from = new MailAddress("from@your.com", "tiger", System.Text.Encoding.UTF8); //发信人信息
MailAddress to = new MailAddress("to@their.com"); //收信人信息
MailMessage message = new MailMessage(from, to);
message.Subject = "这里是主题";
message.Body = "正文内容";
message.SubjectEncoding = System.Text.Encoding.UTF8;
message.BodyEncoding = System.Text.Encoding.UTF8;
smtp.Send(message);
message.Dispose();
}
相关文章推荐
- PHP中发送邮件的几种方法总结
- [总结]发送邮件共有几种方法,测试成功的例子
- 发送邮件的几种方法
- 用C#发送邮件的几种方法
- asp发送邮件的几种方法
- PHP中发送邮件的几种方法总结
- 邮件发送解决方案在.NET环境下几种不同方法示例
- PHP中发送邮件的几种方法总结
- .NET环境下几种不同的邮件发送解决方案
- IOS开发-发送邮件(E-mail)方法整理合集(共3种)
- .NET环境下几种不同的邮件发送解决方案
- 使用sqlserver存储过程sp_send_dbmail发送邮件配置方法
- asp.net一个实现发送邮件的方法
- python发送邮件方法
- python通过smpt发送邮件的方法
- python通过邮件服务器端口发送邮件的方法
- Outlook2003发送投票邮件的方法
- 使用PHP发送邮件的两种方法
- javaMail发送邮件方法
- QTcpSocket 发送数据的几种方法