2014-12-08 191 views
0

我知道有很多關於此問題的問題和解答,我確實閱讀了allot,但它似乎全部過時。通過gmail發送郵件

因此,我有一個移動應用程序,它將使用註冊到雲服務,然後向用戶的電子郵件地址發送歡迎電子郵件。

該服務部在C#WCF女巫做也將郵件發送

下面是用於測試郵件原型功能:

static void SendMail() 
{ 
    var fromAddress = new MailAddress("gmail account", "App name"); 
    var toAddress = new MailAddress("User email", "User account"); 
    const string fromPassword = "gmail password"; 
    const string subject = "test"; 
    const string body = "Hey now!!"; 

    var smtp = new SmtpClient 
    { 
     Host = "smtp.gmail.com", 
     Port = 587, 
     EnableSsl = true, 
     DeliveryMethod = SmtpDeliveryMethod.Network, 
     UseDefaultCredentials = false, 
     Credentials = new NetworkCredential(fromAddress.Address, fromPassword), 
     Timeout = 20000 
    }; 
    using (var message = new MailMessage(fromAddress, toAddress) 
    { 
     Subject = subject, 
     Body = body 
    }) 
    { 
     smtp.Send(message); 
    } 
    Console.WriteLine("Sent"); 
    Console.ReadLine(); 
} 

使用我的代碼全部由別人建議的事情。但我仍然得到錯誤信息

An unhandled exception of type 'System.Net.Mail.SmtpException' occurred in System.dll 

Additional information: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at 
+0

嘗試加入'{如果(String.IsNullOrEmpty(Sendmail的) ) return; }。對你的函數。我不知道,但 – BNN 2014-12-08 09:25:43

+0

沒有改變我得到'smtp.Send(消息)'行中的錯誤' – Jester 2014-12-08 09:28:01

+0

錯誤是什麼? – BNN 2014-12-08 09:32:11

回答

0
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Net.Mail; 
using System.Threading; 
using System.ComponentModel; 
using System.IO; 
using System.Net.Mime; 


namespace Invoice.WCFService 
{ 
    public class EmailSenser 
    { 

     public static bool SendEmail(string toMail, Stream stream, string mailBody) 
     { 

      bool sent = false; 

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

      mail.From = new MailAddress("[email protected]"); 
      mail.To.Add(toMail); 
      mail.Subject = "Invoice"; 
      //mail.Body = "Please, see attached file"; 
      mail.Body = mailBody; 

      mail.SubjectEncoding = System.Text.Encoding.UTF8; 
      mail.BodyEncoding = System.Text.Encoding.UTF8; 

      ContentType ct = new ContentType(MediaTypeNames.Application.Pdf); 

      Attachment attachment = new Attachment(stream, ct); 
      ContentDisposition disposition = attachment.ContentDisposition; 

      disposition.FileName = DateTime.Now.ToString("dd-MM-yyyy") + ".pdf"; 

      mail.Attachments.Add(attachment); 

      SmtpServer.Port = 587; 
      SmtpServer.Credentials = new System.Net.NetworkCredential("[email protected]", "password"); 
      SmtpServer.EnableSsl = true; 

      try 
      { 
       SmtpServer.Send(mail); 
       sent = true; 
      } 
      catch (Exception sendEx) 
      { 
       System.Console.Write("Error: " + sendEx.Message.ToString()); 
       sent = false; 
      } 
      finally 
      { 
       //DBContext 
      } 
      return sent; 
     } 
    } 
} 

這是我完全工作的代碼

0

使用此代碼...

static void SendMail() 
    { 
     var fromAddress = new MailAddress("fromMail", "App name"); 
     var toAddress = new MailAddress("tomail","app"); 
     const string fromPassword = "passwrd"; 
     const string subject = "test"; 
     const string body = "Hey now!!"; 

     var smtp = new SmtpClient 
     { 
      Host = "smtp.gmail.com", 
      Port = 587, 
      EnableSsl = true, 
      DeliveryMethod = SmtpDeliveryMethod.Network, 
      UseDefaultCredentials = false, 
      Credentials = new NetworkCredential(fromAddress.Address, fromPassword), 
      Timeout = 20000, 
     }; 
     using (var message = new MailMessage(fromAddress, toAddress)) 
     { 
      message.Subject = subject; 
      message.Body = body; 
      smtp.Send(message); 
     } 
     Console.WriteLine("Sent"); 
     Console.ReadLine(); 
    } 
+0

如果我從開發機器運行該錯誤我得到與上面相同的錯誤 – Jester 2014-12-08 09:40:17

+1

開發機器的日期和時間是否準確,您是否可以使用瀏覽器從開發機器使用gmail服務?只是一個嘗試... – 2014-12-08 09:42:38

+0

是時間和日期是正確的,我可以使用gmail – Jester 2014-12-08 09:51:54