2011-03-05 7 views
7

Windows 응용 프로그램에서 Gmail의 사용자 이름과 비밀번호를 사용하여 이메일을 보내려고합니다. 그러나 다음 코드는 StringBuilder 인스턴스에서 여러 전자 메일 주소를 수집 할 때 첫 번째 전자 메일 주소로만 메일을 보냅니다.C#에서 여러 주소로 이메일을 보내는 방법

var fromAddress = new MailAddress(username, DefaultSender); 
var toAddress = new MailAddress(builder.ToString());//builder reference having multiple email address 

string subject = txtSubject.Text; 
string body = txtBody.Text; ; 
var smtp = new SmtpClient 
{ 
    Host = HostName, 
    Port = 587, 
    EnableSsl = true, 
    DeliveryMethod = SmtpDeliveryMethod.Network, 
    UseDefaultCredentials = false, 
    Credentials = new NetworkCredential(username, password), 
    //Timeout = 1000000 

}; 
var message = new MailMessage(fromAddress, toAddress) 
{ 
    Subject = subject, 
    Body = body, 
    IsBodyHtml = chkHtmlBody.Checked 

}; 

if (System.IO.File.Exists(txtAttechments.Text)) 
{ 
    System.Net.Mail.Attachment attechment = new Attachment(txtAttechments.Text); 
    message.Attachments.Add(attechment); 
} 

if(this.Enabled) 
    this.Enabled = false; 

smtp.Send(message); 

무엇이 잘못 되었나요? 그리고 어떻게 문제를 해결할 수 있습니까?

+0

합니까에서 작동하지 않습니다

message.To.Add(new MailAddress("[email protected], [email protected]")); 

이를 사용하는 경우이 하나가 .NET 3.5

에서 일할 수 실제로

message.To.Add("[email protected], [email protected]"); 

입니다 빌더는 ","또는 ";"로 구분됩니까? – CarneyCode

+0

빌더 인스턴스가 이메일 주소 ","로 구분 한 : "이메일 1 @ gmail.com, EMAIL2 @ gmail.com" – Joe

답변

4

가장 좋은 방법은 개별적으로 MailAddress ES의 message.To.Add() 각이다. .NET의 초기 버전은 쉼표 또는 세미콜론으로 구분 된 전자 메일 주소를 구문 분석하는 것이 더 최신 런타임 버전보다 더 행복하다고 생각합니다.

+0

message.To.Add (builder.ToString()) 코드는 잘 작동한다. 감사! – Joe

0

저도 같은 문제가되었다.

는 코드는 .NET 3.5

관련 문제