호스트에게 이메일을 보내지 않고 Gmail 계정을 사용하여 이메일 메시지를 보내려고했습니다 . 이메일은 제가 쇼에서 연주하는 밴드에 대한 개인화 된 이메일입니다.
그것을 할 수 있습니까?
답변
반드시 사용할 수 System.Net.Mail, 사용되지 않는 없습니다 System.Web.Mail. SSL을 사용하는 System.Web.Mail것은 해키 확장의 심각한 혼란입니다.
using System.Net;
using System.Net.Mail;
var fromAddress = new MailAddress("from@gmail.com", "From Name");
var toAddress = new MailAddress("to@example.com", "To Name");
const string fromPassword = "fromPassword";
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);
}답변
위의 답변이 작동하지 않습니다. DeliveryMethod = SmtpDeliveryMethod.Network” 클라이언트가 인증되지 않았습니다 “오류 와 함께 설정해야합니다 . 또한 시간 초과를 설정하는 것이 좋습니다.
수정 된 코드 :
using System.Net.Mail;
using System.Net;
var fromAddress = new MailAddress("from@gmail.com", "From Name");
var toAddress = new MailAddress("to@yahoo.com", "To Name");
const string fromPassword = "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,
    Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
    Timeout = 20000
};
using (var message = new MailMessage(fromAddress, toAddress)
{
    Subject = subject,
    Body = body
})
{
    smtp.Send(message);
}답변
다른 답변이 “서버에서”작동하려면 먼저 Gmail 계정의 보안 수준이 낮은 앱 에 대한 액세스를 설정 하십시오.
최근 Google이 보안 정책을 변경 한 것 같습니다. https://support.google.com/accounts/answer/6010255?hl=ko-GB에 설명 된대로 계정 설정을 변경하기 전까지는 최고 등급의 답변이 더 이상 작동하지 않습니다.
2016 년 3 월부터 Google은 설정 위치를 다시 변경했습니다!
답변
첨부 파일과 함께 이메일을 보내는 것입니다.
출처 : http://coding-issues.blogspot.in/2012/11/sending-email-with-attachments-from-c.html
using System.Net;
using System.Net.Mail;
public void email_send()
{
    MailMessage mail = new MailMessage();
    SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
    mail.From = new MailAddress("your mail@gmail.com");
    mail.To.Add("to_mail@gmail.com");
    mail.Subject = "Test Mail - 1";
    mail.Body = "mail with attachment";
    System.Net.Mail.Attachment attachment;
    attachment = new System.Net.Mail.Attachment("c:/textfile.txt");
    mail.Attachments.Add(attachment);
    SmtpServer.Port = 587;
    SmtpServer.Credentials = new System.Net.NetworkCredential("your mail@gmail.com", "your password");
    SmtpServer.EnableSsl = true;
    SmtpServer.Send(mail);
}답변
Google은 최신 보안 표준을 사용하지 않는 일부 앱 또는 기기의 로그인 시도를 차단할 수 있습니다. 이러한 앱과 기기는 쉽게 침입 할 수 있으므로 차단하면 계정을보다 안전하게 유지할 수 있습니다.
최신 보안 표준을 지원하지 않는 앱의 일부 예는 다음과 같습니다.
- iOS 6 이하가 설치된 iPhone 또는 iPad의 Mail 앱
- 8.1 릴리스 이전의 Windows 폰의 메일 앱
- Microsoft Outlook 및 Mozilla Thunderbird와 같은 일부 데스크탑 메일 클라이언트
따라서 Google 계정에서 덜 안전한 로그인 을 사용하도록 설정 해야합니다.
Google 계정에 로그인 한 후 다음으로 이동하십시오.
https://myaccount.google.com/lesssecureapps 
또는 
https://www.google.com/settings/security/lesssecureapps 
C #에서는 다음 코드를 사용할 수 있습니다.
using (MailMessage mail = new MailMessage())
{
    mail.From = new MailAddress("email@gmail.com");
    mail.To.Add("somebody@domain.com");
    mail.Subject = "Hello World";
    mail.Body = "<h1>Hello</h1>";
    mail.IsBodyHtml = true;
    mail.Attachments.Add(new Attachment("C:\\file.zip"));
    using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))
    {
        smtp.Credentials = new NetworkCredential("email@gmail.com", "password");
        smtp.EnableSsl = true;
        smtp.Send(mail);
    }
}답변
내가 작동하게하려면 다른 앱이 액세스 할 수 있도록 Gmail 계정을 활성화해야했습니다. “보안 수준이 낮은 앱 사용” 을 사용하고 https://accounts.google.com/b/0/DisplayUnlockCaptcha 링크를 사용하면됩니다.
답변
내 버전은 다음과 같습니다. ” C #에서 Gmail로 이메일 보내기 “.
using System;
using System.Net;
using System.Net.Mail;
namespace SendMailViaGmail
{
   class Program
   {
   static void Main(string[] args)
   {
      //Specify senders gmail address
      string SendersAddress = "Sendersaddress@gmail.com";
      //Specify The Address You want to sent Email To(can be any valid email address)
      string ReceiversAddress = "ReceiversAddress@yahoo.com";
      //Specify The password of gmial account u are using to sent mail(pw of sender@gmail.com)
      const string SendersPassword = "Password";
      //Write the subject of ur mail
      const string subject = "Testing";
      //Write the contents of your mail
      const string body = "Hi This Is my Mail From Gmail";
      try
      {
        //we will use Smtp client which allows us to send email using SMTP Protocol
        //i have specified the properties of SmtpClient smtp within{}
        //gmails smtp server name is smtp.gmail.com and port number is 587
        SmtpClient smtp = new SmtpClient
        {
           Host = "smtp.gmail.com",
           Port = 587,
           EnableSsl = true,
           DeliveryMethod = SmtpDeliveryMethod.Network,
           Credentials    = new NetworkCredential(SendersAddress, SendersPassword),
           Timeout = 3000
        };
        //MailMessage represents a mail message
        //it is 4 parameters(From,TO,subject,body)
        MailMessage message = new MailMessage(SendersAddress, ReceiversAddress, subject, body);
        /*WE use smtp sever we specified above to send the message(MailMessage message)*/
        smtp.Send(message);
        Console.WriteLine("Message Sent Successfully");
        Console.ReadKey();
     }
     catch (Exception ex)
     {
        Console.WriteLine(ex.Message);
        Console.ReadKey();
     }
    }
   }
 }
