邮件发送_文本发送

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyMail
{
    public partial class Form1 : Form
    {
        public string smtp = "smtp.163.com";//这个是163的格式。
        public string from = "fromMail@163.com";//发件人邮箱
        public string pwd = "fromMailPwd";//发件人密码
        public string to = "toMail@163.com";//邮件接收人
        public string subject = "测试";//邮件标题
        public string body = "内容11111111111" +System.DateTime.Now;//邮件内容

        public Form1()
        {
            InitializeComponent();
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            SendMail2();
        }

        public void SendMail2()
        {
            //创建smtpclient对象
            System.Net.Mail.SmtpClient client = new SmtpClient();
            client.Host = smtp;
            client.UseDefaultCredentials = false;
            client.Credentials = new System.Net.NetworkCredential(from, pwd);
            client.DeliveryMethod = SmtpDeliveryMethod.Network;

            //创建mailMessage对象 
            System.Net.Mail.MailMessage message = new MailMessage(from, to);
            message.Subject = subject;
            //正文默认格式为html
            message.Body = body;
            message.IsBodyHtml = true;
            message.BodyEncoding = System.Text.Encoding.UTF8;            

            try
            {
                client.Send(message);                
            }
            catch (Exception ex)
            {
               
            }
        }
    }
}

 

posted @ 2014-05-22 13:18  pnljs  阅读(315)  评论(0编辑  收藏  举报