1

我想使用 Gmail 发送电子邮件,但出现以下异常

尝试对无法访问的网络进行套接字操作 [2a00:1450:400c:c05::6d]:465

你知道如何解决它吗?

MailMessage msg = new MailMessage();
msg.From = new MailAddress("test@yahoo.com");
msg.To.Add("test@yahoo.com");
msg.Subject = this.txtSubject.Text;
msg.Body = this.txtBody.Text;

SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Port = 465;
client.EnableSsl = true;
client.Send(msg);
4

3 回答 3

0

尝试这个

using System.Net;

using System.Net.Mail;

 var fromAddress = new MailAddress("from@gmail.com", "From Name");
 var toAddress = new MailAddress("to@test.com", "To Name");
 const string fromPassword = "fromPass";
const string subject = "Subject";
const string body = "Body";

 var smtp = new SmtpClient
       {
           Host = "smtp.gmail.com",
           Port = 587,
           EnableSsl = true,
           DeliveryMethod = SmtpDeliveryMethod.Network,
           UseDefaultCredentials = false,
           Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
       };
  using (var message = new MailMessage(fromAddress, toAddress)
                 {
                     Subject = subject,
                     Body = body
                 })
   {
smtp.Send(message);
  }
于 2013-06-04T13:17:25.587 回答
0

添加:

NetworkCredential basicCredential = 
    new NetworkCredential("username", "password"); 

smtpClient.Host = "mail.mydomain.com";
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = basicCredential;

(从How can I make SMTP authentication in C# 中采用)

于 2013-06-04T13:16:12.597 回答
0

我已经这样做了:

使用这些用法:

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;

而这个按钮事件

 private void sendmail_Click(object sender, EventArgs e)
    {
        try
        {

            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

            mail.From = new MailAddress("blabla@gmail.com");
            mail.To.Add("mom@gmail.com");
            mail.Subject = "something";
            mail.Body = "text that is in the mail";
//for attachements
            System.Net.Mail.Attachment attachment;
            attachment = new System.Net.Mail.Attachment(str);
            mail.Attachments.Add(attachment);
//end attachement

            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential"username from gmail", "password from gmail");
            SmtpServer.EnableSsl = true;

            SmtpServer.Send(mail);
            MessageBox.Show("Mail Send");
            this.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }
于 2013-06-04T14:12:42.763 回答