Email邮件发送程序

using System.Net.Mail;<br><br><strong>方法一:向单个地址发送邮件,不设置web.config文件<br></strong><font>public void SendMail()<br> {<br> string mailto = "to@company.com";<br> string mailfrom = "from@company.com";</font>
<p><font> System.Net.NetworkCredential credential = new System.Net.NetworkCredential("from_username", "from_password");<br> SmtpClient smtp = new SmtpClient("smtp.company.com");<br> smtp.Credentials = credential;</font></p>
<p><font> MailMessage message = new MailMessage(mailfrom, mailto);<br> message.SubjectEncoding = System.Text.Encoding.UTF8;<br> message.BodyEncoding = System.Text.Encoding.UTF8;<br> message.Subject = "subject here";<br> message.Body = "body here";<br> smtp.Send(message);<br> message.Dispose();<br> }<br><br><strong>方法二、向单个地址发送邮件,设置web.config文件</strong><br><font>public void SendMail()<br> {<br> string mailto = "to@company.com";<br> string mailfrom = "from@company.com";</font> </font></p>
<p><font> MailMessage message = new MailMessage(mailfrom, mailto);<br> message.SubjectEncoding = System.Text.Encoding.UTF8;<br> message.BodyEncoding = System.Text.Encoding.UTF8;<br> message.Subject = "subject here";<br> message.Body = "body here";<br> smtp.Send(message);<br> message.Dispose();<br> }<br><br> 在web.config中添加如下:<br><font><system.net><br> <mailSettings><br> <smtp from="from@company.com"><br> <network host="smtp.company.com" port="25" userName="from_username" password="from_password"/><br> </smtp><br> </mailSettings><br> </system.net><br><br><strong>方法三:群发邮件,设置web.config文件</strong><br><font>public void SendEmail()<br> {<br> string mailto = "to1@company.com,to2@company.com";<br> string title = "mail title here";<br> string content = "mail content here";</font></font></font></p>
<p><font> SmtpClient smtp = new SmtpClient();<br> MailMessage message = new MailMessage();<br> MailAddressCollection address = new MailAddressCollection();<br> string[] mailtos = mailto.Split(',');<br> for (int i = 0; i < mailtos.Length; i++)<br> {<br> address.Add(mailtos[i]);<br> }<br> foreach (MailAddress add in address)<br> {<br> message.To.Add(add);<br> }<br> message.SubjectEncoding = System.Text.Encoding.UTF8;<br> message.BodyEncoding = System.Text.Encoding.UTF8;<br> message.Subject = title;<br> message.Body = content;<br> smtp.Send(message);<br> message.Dispose();<br> address.Clear();<br> }<br><br> 在web.config中添加如下:<br><font><system.net><br> <mailSettings><br> <smtp from="from@company.com"><br> <network host="smtp.company.com" port="25" userName="from_username" password="from_password"/><br> </smtp><br> </mailSettings><br> </system.net><br><br> 采用以上方法,如果运行发信程序的计算机上装有邮件监控等杀毒软件,会有失败的警告,但实际已发送成功。解决办法是关闭杀毒软件的监控功能。</font></font></p>
×××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××× <br>方法四<br>using System.Web.Mail;<br><br> public void sendMail()<br> {<br> MailMessage mail1 = new MailMessage();<br> mail1.Body="body here";<br> mail1.From="xxx@xxx.com";<br> mail1.To="yyy@yyy.com";<br> mail1.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",1);<br> mail1.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername","xxx@xxx.com");<br> mail1.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword","********");<br> SmtpMail.SmtpServer="mail.xxx.com";<br> SmtpMail.Send(mail1);<br> }<br><br> 以上添加的几个 Fields 是用来作SMTP发信认证的,如果你的发信服务器不需要认证,就可以省略这几句。<br>××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值