您当前的位置: 首页 >  c#

彭世瑜

暂无认证

  • 1浏览

    0关注

    2791博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

C#编程-140:Net.Mail类发送邮件_彭世瑜_新浪博客

彭世瑜 发布时间:2017-08-22 22:20:11 ,浏览量:1

C#编程-140:Net.Mail类发送邮件 C#编程-140:Net.Mail类发送邮件 C#编程-140:Net.Mail类发送邮件 C#编程-140:Net.Mail类发送邮件 C#编程-140:Net.Mail类发送邮件 C#编程-140:Net.Mail类发送邮件 C#编程-140:Net.Mail类发送邮件 C#编程-140:Net.Mail类发送邮件
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Net.Mail;
  10.  
  11. namespace MailTest
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.  
  20.         private void btnSend_Click(object sender, EventArgs e)
  21.         {
  22.             MailMessage mail = new MailMessage();
  23.             string myEmail = txtAccount.Text;
  24.             string myPwd = txtPassword.Text;
  25.             mail.BodyEncoding = System.Text.Encoding.UTF8;
  26.             mail.IsBodyHtml = true;
  27.             mail.From = new MailAddress(myEmail);
  28.             mail.To.Add(new MailAddress(txtAccept.Text));
  29.             mail.Subject = txtSubject.Text;
  30.             mail.Body = txtBody.Text;
  31.             mail.BodyEncoding = Encoding.UTF8;
  32.             SmtpClient client = new SmtpClient(txtServer.Text);
  33.             client.UseDefaultCredentials = false;
  34.             client.Credentials = new System.Net.NetworkCredential(myEmail.Substring(0,myEmail.IndexOf('@')),myPwd);
  35.             client.DeliveryMethod = SmtpDeliveryMethod.Network;
  36.             try
  37.             {
  38.                 client.Send(mail);
  39.                 MessageBox.Show("发送成功!");
  40.                 txtSubject.Text = null;
  41.                 txtBody.Text = null;
  42.             }
  43.             catch (Exception ex)
  44.             {
  45.                 MessageBox.Show(ex.Message);
  46.                  
  47.             }
  48.         }     
  49.     }
  50. }
关注
打赏
1665367115
查看更多评论
立即登录/注册

微信扫码登录

0.0702s