none
winform 发送邮件的问题 RRS feed

  • 问题

  • private void button2_Click(object sender, EventArgs e) { MailMessage msg = new MailMessage(); msg.Subject = this.textBox2.Text; //标题 msg.Body = this.textBox3.Text; //内容 msg.From = new MailAddress("ZYQ187@163.com"); msg.To.Add(this.textBox4.Text); SmtpClient client = new SmtpClient(); client.Host = "smtp.163.com"; //发件方服务器地址 client.Port = 25; //发件方端口 NetworkCredential credential = new NetworkCredential(); credential.UserName = "ZYQ187@163.com"; credential.Password = "12121212"; client.Credentials = credential; client.Send(msg); }

    这是我用的代码,基本就是一个winform上三个textbox,题目,接收者和内容。然后有个button发送。

    这段代码也没有报错但是我调试的时候显示超时,System.Net.Mail.SmtpException: 'The operation has timed out.'

    不知道是因为我邮箱的问题还是什么?请教大神,感恩

    2018年5月17日 6:17

全部回复

  • 试下这几句

    using System.Net.Mail;
    
    ...
    
    MailMessage mail = new MailMessage("you@yourcompany.com", "user@hotmail.com");
    SmtpClient client = new SmtpClient();
    client.Port = 25;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.UseDefaultCredentials = false;
    client.Host = "smtp.gmail.com";
    mail.Subject = "this is a test email.";
    mail.Body = "this is my test email body";
    client.Send(mail);


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    2018年5月18日 0:28
  • System.Net.Mail.SmtpException: 'The operation has timed out.'
    还是显示超时呢,不知道是什么原因,难道是因为我用的公司的笔记本?听说公司会对电脑做一些控制?还是因为我代码或者邮箱本身有问题?
    2018年5月18日 1:42
  • 你好,

    设置一下EnableSsl = ture, 在试试看,下面的代码,你可以参考。

    System.Net.NetworkCredential aCred = new System.Net.NetworkCredential("myacct", "mypassword");
    
    SmtpClient smtp = new SmtpClient("smtp.mail.myserver.com", 465);
    
    smtp.EnableSsl = true;
    
    smtp.UseDefaultCredentials = false;
    
    smtp.Credentials = aCred;

    详细的信息,你可以参考下面的链接:

    https://blogs.msdn.microsoft.com/webdav_101/2008/06/02/system-net-mail-with-ssl-to-authenticate-against-port-465/

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年5月18日 8:21
    版主
  • 试了一下还是不行,我不知道这个是不是和公司的防火墙有关系?但是我用的是私人邮箱。。。。。
            private void button2_Click(object sender, EventArgs e)
            {
                MailMessage msg = new MailMessage();
    
                msg.Subject = this.textBox2.Text;   
                msg.Body = this.textBox3.Text;   
                msg.From = new MailAddress("187@163.com");
                msg.To.Add(this.textBox4.Text);
                System.Net.NetworkCredential aCred = new System.Net.NetworkCredential("187@163.com", "zAD0");
                SmtpClient client = new SmtpClient();
                client.Host = "smtp.163.com";   //发件方服务器地址
                client.Port = 465;  //发件方端口
                client.EnableSsl = true;
                client.UseDefaultCredentials = false;
                client.Credentials = aCred;
                client.Send(msg);
    
            }

    2018年5月21日 8:17
  • 163要邮箱要按照它的要求启用smtp 服务,默认smtp服务是关闭的。


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    2018年5月22日 0:40
  • 我应该是启用SMTP服务的,启用之后还让我设置了一个密码,和登陆密码是不一样的,这个启用的密码我有写在代码里。而且我前面用了QQ邮箱试验,应该是没开SMTP服务他显示的不是超时的报错而是连接不到你所写的邮箱。
    2018年5月23日 1:43
  • hi,我使用你的代码并稍微改了一些,可以发送啊

    please verify my account

    2018年6月22日 4:38
  • MailMessage msg = new MailMessage();
                    msg.Subject = "测试";   //标题
                    msg.Body = " 测试,测试";    //内容
                    msg.From = new MailAddress("163邮箱");
                    msg.To.Add("发送地址");
                    SmtpClient client = new SmtpClient();
                    client.Host = "smtp.163.com";   //发件方服务器地址
                    client.Port = 25;  //发件方端口
                    client.UseDefaultCredentials = true;
                    NetworkCredential credential = new NetworkCredential("163邮箱", "密码");
                    client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                    credential.UserName = "用户名";
                    credential.Password = "密码";
                    client.Credentials = credential;
                    client.Send(msg);


    please verify my account

    2018年6月22日 4:39