2014-12-08 3 views
0

나는 이것에 대한 많은 질문과 답변을 알고 있으며 할당량을 읽었지 만 모두 구식 인 것 같습니다.Gmail을 통해 메일 보내기

그래서 모바일 서비스를 사용하여 클라우드 서비스에 등록한 다음 사용자의 이메일 주소로 환영 이메일을 전송합니다.

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(); 
} 

내 코드를 사용하는 다른 사람에 의해 제안 모든 것 : 서비스 부분은 C# WCF 마녀에서 이루어집니다

은 아래 메일에 대한 테스트를위한 프로토 타입 기능은 메일

에게 보냅니다. 하지만 난 여전히 오류 메시지가

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 (센드)) 반환; }'. 당신의 함수에. 잘 모르겠지만 – BNN

+0

변경 사항 없음 'smtp.Send (message); 줄에 오류가 나타납니다.' – Jester

+0

무엇이 오류입니까? – BNN

답변

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

+1

dev 머신의 날짜와 시간이 정확하고 브라우저를 사용하는 dev 머신에서 gmail 서비스를 사용할 수 있습니까? 그냥 시도하십시오 ... –

+0

예 시간과 날짜가 정확하며 Gmail을 사용할 수 있습니다 – Jester

관련 문제