11

.net中发送邮件的方法(C#)

 3 years ago
source link: https://www.cnblogs.com/tuyile006/archive/2007/06/15/784446.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

.net中发送邮件的方法(C#)


.Net中发送邮件的方法如下:


System.Net.Mail.SmtpClient client = new SmtpClient();
            client.Host = "smtp.163.com";
            client.UseDefaultCredentials = false;
            client.Credentials = new System.Net.NetworkCredential("你的163用户名", "*****");
            //星号改成自己邮箱的密码
            client.DeliveryMethod = SmtpDeliveryMethod.Network;

            System.Net.Mail.MailMessage message = new MailMessage("你的163邮箱地址", "收件人邮箱地址");
            message.Subject = "测试";
            message.Body = "用自己写的软件发的邮件!";
            message.BodyEncoding = System.Text.Encoding.UTF8;
            message.IsBodyHtml = true;
           //添加附件
            Attachment data = new Attachment(@"附件地址如:e:\a.jpg", System.Net.Mime.MediaTypeNames.Application.Octet);
            message.Attachments.Add(data);

            try
            {
                client.Send(message);
                MessageBox.Show("Email successfully send.");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Send Email Failed." + ex.ToString());
            }


测试一下就知道是否可用了。

用上面介绍的方法实现C#邮件发送小程序

界面设计如下:

mails.jpg
  演示版下载:Demo

在此声明:本程序纯技术交流,不要用作非法用途。

关于发送时报错或发不出去:如果是2.0版本请给framework打sp1补丁。然后检查smtp服务器,和发送方邮箱帐号密码是否正确,填写的发送人的邮箱地址一定要跟smtp服务器是同一家服务商,如果stmp服务器是sina服务器,就写sina邮箱,如果是163服务器就都写163邮箱。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK