Email邮件发送程序

using System.Net.Mail;

方法一:向单个地址发送邮件,不设置web.config文件
public void SendMail()
{
        string mailto = "to@company.com";
        string mailfrom = "from@company.com";

        System.Net.NetworkCredential credential = new System.Net.NetworkCredential("from_username", "from_password");
        SmtpClient smtp = new SmtpClient("smtp.company.com");
        smtp.Credentials = credential;

        MailMessage message = new MailMessage(mailfrom, mailto);
        message.SubjectEncoding = System.Text.Encoding.UTF8;
        message.BodyEncoding = System.Text.Encoding.UTF8;
        message.Subject = "subject here";
        message.Body = "body here";
        smtp.Send(message);
        message.Dispose();
}

方法二、向单个地址发送邮件,设置web.config文件
public void SendMail()
 {
        string mailto = "to@company.com";
        string mailfrom = "from@company.com";

        MailMessage message = new MailMessage(mailfrom, mailto);
        message.SubjectEncoding = System.Text.Encoding.UTF8;
        message.BodyEncoding = System.Text.Encoding.UTF8;
        message.Subject = "subject here";
        message.Body = "body here";
        smtp.Send(message);
        message.Dispose();
}

在web.config中添加如下:
<system.net>
        <mailSettings>
                <smtp from="from@company.com">
                        <network host="smtp.company.com" port="25" userName="from_username" password="from_password"/>
                </smtp>
        </mailSettings>
</system.net>

方法三:群发邮件,设置web.config文件
public void SendEmail()
{
        string mailto = "to1@company.com,to2@company.com";
        string title = "mail title here";
        string content = "mail content here";

        SmtpClient smtp = new SmtpClient();
        MailMessage message = new MailMessage();
        MailAddressCollection address = new MailAddressCollection();
        string[] mailtos = mailto.Split(',');
        for (int i = 0; i < mailtos.Length; i++)
        {
            address.Add(mailtos[i]);
        }
        foreach (MailAddress add in address)
        {
            message.To.Add(add);
        }
        message.SubjectEncoding = System.Text.Encoding.UTF8;
        message.BodyEncoding = System.Text.Encoding.UTF8;
        message.Subject = title;
        message.Body = content;
        smtp.Send(message);
        message.Dispose();
        address.Clear();
}

在web.config中添加如下:
<system.net>
        <mailSettings>
                <smtp from="from@company.com">
                        <network host="smtp.company.com" port="25" userName="from_username" password="from_password"/>
                </smtp>
        </mailSettings>
</system.net>

采用以上方法,如果运行发信程序的计算机上装有邮件监控等杀毒软件,会有失败的警告,但实际已发送成功。解决办法是关闭杀毒软件的监控功能。

××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
方法四
using System.Web.Mail;

public void sendMail()
{
        MailMessage mail1 = new MailMessage();
        mail1.Body="body here";
        mail1.From="xxx@xxx.com";
        mail1.To="yyy@yyy.com";
        mail1.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",1);
        mail1.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername","xxx@xxx.com");
        mail1.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword","********");
        SmtpMail.SmtpServer="mail.xxx.com";
        SmtpMail.Send(mail1);
}

以上添加的几个 Fields 是用来作SMTP发信认证的,如果你的发信服务器不需要认证,就可以省略这几句。
××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值