JMail组件是否可通过GMail的Smtp发送邮件??

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Net.Mail;namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                sendSMforGMail("[email protected]", "akdkdls", "李明", "测试一下", "近来好吗?", "[email protected]");
            }
            private string sendSMforGMail(string sender, string pwd, string name, string SMtitle, string SMContent, string receiver)
            {
                //范例 sendSMforGMail("[email protected]","李明","测试一下","近来好吗?","[email protected]")
                System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
                // msg.To.Add(phonenumber + "@139.com");
                msg.To.Add(receiver);
                //msg.To.Add("");
                //msg.To.Add("[email protected]");
                /* 
                 * msg.To.Add("[email protected]"); 
                 * msg.To.Add("[email protected]"); 
                 * msg.To.Add("[email protected]");可以发送给多人 
                 */
                //msg.CC.Add("抄送地址");
                /* 
                 * msg.CC.Add("[email protected]"); 
                 * msg.CC.Add("[email protected]");可以抄送给多人 
                 */
                msg.From = new MailAddress(sender, name, System.Text.Encoding.UTF8);
                /* 上面3个参数分别是发件人地址(可以随便写),发件人姓名,编码*/
                msg.Subject = SMtitle;//邮件标题             
                msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码 
                msg.Body = SMContent;//邮件内容 
                msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码 
                msg.IsBodyHtml = false;//是否是HTML邮件 
                // msg.Priority = MailPriority.High;//邮件优先级             SmtpClient client = new SmtpClient();
                client.Credentials = new System.Net.NetworkCredential(sender, pwd);
                //上述写你的邮箱和密码 
                client.Port = 587;//gmail 使用的端口 
                client.Host = "smtp.gmail.com";
                client.EnableSsl = true;//经过ssl加密 
                object userState = msg;
                try
                {
                    //client.SendAsync(msg, userState);
                    client.Send(msg);
                    //简单一点儿可以client.Send(msg); 
                    // MessageBox.Show("发送成功");
                    //textBox2.Text = textBox2.Text + DateTime.Now.ToString() + " Have sent a message!" + "/r/n";
                    return "1";
                }
                catch (System.Net.Mail.SmtpException ex)
                {
                    MessageBox.Show(ex.Message, "发送邮件出错");
                    // textBox2.Text = textBox2.Text + DateTime.Now.ToString() + " " + ex.Message + "/r/n";
                    return "0";
                }
            }
        }
    }