Email邮件发送程序

来源:互联网 发布:校园二手交易 php源码 编辑:程序博客网 时间:2024/06/11 01:01
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发信认证的,如果你的发信服务器不需要认证,就可以省略这几句。
××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
原创粉丝点击