Google Apps 계정을 통해 C #을 통해 이메일 보내기
표준 Google Apps 계정이 있습니다. Google Apps를 통해 맞춤 도메인을 설정했습니다. Gmail 인터페이스를 사용할 때 Google Apps를 통해 성공적으로 이메일을주고받을 수 있습니다. 하지만 코드를 통해 이메일을 보내고 싶습니다. 이를 시도하기 위해 다음 코드를 시도했습니다.
MailMessage mailMessage = new MailMessage();
mailMessage.To.Add("someone@somewhere.com");
mailMessage.Subject = "Test";
mailMessage.Body = "<html><body>This is a test</body></html>";
mailMessage.IsBodyHtml = true;
// Create the credentials to login to the gmail account associated with my custom domain
string sendEmailsFrom = "emailAddress@mydomain.com";
string sendEmailsFromPassword = "password";
NetworkCredential cred = new NetworkCredential(sendEmailsFrom, sendEmailsFromPassword);
SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);
mailClient.EnableSsl = true;
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailClient.UseDefaultCredentials = false;
mailClient.Timeout = 20000;
mailClient.Credentials = cred;
mailClient.Send(mailMessage);
Send 메서드에 도달하면 다음과 같은 예외가 발생합니다.
"SMTP 서버에 보안 연결이 필요하거나 클라이언트가 인증되지 않았습니다. 서버 응답 : 5.5.1 인증 필요."
Google을 통해 내 사용자 정의 도메인을 통해 이메일을 보내려면 어떻게합니까?
감사!
코드의 모든 SMTP 설정을 하드 코딩 할 필요가 없습니다. 대신 web.config에 넣으십시오. 이렇게하면 필요한 경우 이러한 설정을 암호화하고 응용 프로그램을 다시 컴파일하지 않고 즉시 변경할 수 있습니다.
<configuration>
<system.net>
<mailSettings>
<smtp from="example@domain.com" deliveryMethod="Network">
<network host="smtp.gmail.com" port="587"
userName="example@domain.com" password="password"/>
</smtp>
</mailSettings>
</system.net>
</configuration>
이메일을 보낼 때 끝납니다. SmtpClient에서 SSL을 활성화하십시오.
var message = new MailMessage("navin@php.net");
// here is an important part:
message.From = new MailAddress("example@domain.com", "Mailer");
// it's superfluous part here since from address is defined in .config file
// in my example. But since you don't use .config file, you will need it.
var client = new SmtpClient();
client.EnableSsl = true;
client.Send(message);
Gmail에서 인증하려는 이메일 주소에서 이메일을 보내고 있는지 확인하세요.
참고 : .NET 4.0부터는 코드에서 설정하는 대신 enableSsl = "true"를 web.config에 삽입 할 수 있습니다.
이것이 내가 WPF 4에서 사용하는 것입니다.
SmtpClient client = new SmtpClient();
client.Credentials = new NetworkCredential("sender_email@domain.tld", "P@$$w0rD");
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
try {
MailAddress
maFrom = new MailAddress("sender_email@domain.tld", "Sender's Name", Encoding.UTF8),
maTo = new MailAddress("recipient_email@domain2.tld", "Recipient's Name", Encoding.UTF8);
MailMessage mmsg = new MailMessage(maFrom.Address, maTo.Address);
mmsg.Body = "<html><body><h1>Some HTML Text for Test as BODY</h1></body></html>";
mmsg.BodyEncoding = Encoding.UTF8;
mmsg.IsBodyHtml = true;
mmsg.Subject = "Some Other Text as Subject";
mmsg.SubjectEncoding = Encoding.UTF8;
client.Send(mmsg);
MessageBox.Show("Done");
} catch (Exception ex) {
MessageBox.Show(ex.ToString(), ex.Message);
//throw;
}
방화벽 및 안티 바이러스를주의하십시오. 이러한 소름 끼치는 것은 이메일을 보내는 애플리케이션을 차단하는 경향이 있습니다. McAfee Enterprise를 사용하고 이메일을 보낼 수 있도록 실행 파일 이름 (Bazar.exe 및 Bazar.vshost.exe 모두에 대해 Bazar. *와 같은)을 추가해야합니다 ...
포트를 465로 변경
아무것도 할 필요가 없습니다. 현재 계정에 처음 로그인하고 지침을 따르십시오.
Your problem will resolve. It occur because you had created the account in google apps but did not login. Just login and follow the instructions and try.
My code is connecting to smtp.google.com using TLS on Port=587 (SSL should be port 465) but still needs EnableSsl=true
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
NetworkCredential credentials = new NetworkCredential();
credentials.UserName = "INSERT EMAIL";
credentials.Password = "INSERT PASSWORD";
smtp.Credentials = credentials;
MailAddress addressFrom = new MailAddress(credentials.UserName);
MailAddress addressTo = new MailAddress("INSERT RECIPIENT");
MailMessage msg = new MailMessage(addressFrom, addressTo);
msg.Subject = "SUBJECT"
msg.Body = "BODY";
smtp.Send(msg);
Notice these important prerequisites on your G SUITE account
- Ensure that the username you use has cleared the CAPTCHA word verification test that appears when you first sign in.
- Ensure that the account has a secure password - https://support.google.com/accounts/answer/32040
- Make sure that Less secure apps is enabled for the desired account- https://support.google.com/a/answer/6260879
참고URL : https://stackoverflow.com/questions/757987/send-email-via-c-sharp-through-google-apps-account
'Development Tip' 카테고리의 다른 글
Python에서 memoryview의 요점은 정확히 무엇입니까 (0) | 2020.11.21 |
---|---|
스크롤하는 동안 Jquery .on ( 'scroll')이 이벤트를 발생시키지 않습니다. (0) | 2020.11.21 |
Automapper를 사용하여 목록 매핑 (0) | 2020.11.21 |
쿠키가 활성화되어 있는지 확인 (0) | 2020.11.21 |
수학 표현을 단순화하기위한 전략 (0) | 2020.11.21 |