Monday, March 26, 2012

System.Net.Mail to address

I would like to have an email go to 2 email addresses but when I try it by
separating with semicolon or comma it fails. Can anyone help? below is my
code (see ToAddress string). Thanks.

David
strUser = UtilClass.GetUserName(strUser)

'!!! UPDATE THIS VALUE TO YOUR EMAIL ADDRESS
Const ToAddress As String = "me@dotnet.itags.org.myemail.com"
Const FromAddress As String = "me@dotnet.itags.org.myemail.com"

'(1) Create the MailMessage instance
Dim mm As New System.Net.Mail.MailMessage(ToAddress,
FromAddress)

'(2) Assign the MailMessage's properties
mm.Subject = "Fileroom unhandled exception occurred!"
mm.Body = "User getting error is " & strUser & vbCrLf & _
String.Format("An unhandled exception
occurred:{0}Message: {1}{0}{0} Stack Trace:{0}{2}",
System.Environment.NewLine, ex.Message, ex.StackTrace)
mm.IsBodyHtml = False

'(3) Create the SmtpClient object
Dim smtp As New System.Net.Mail.SmtpClient

'(4) Send the MailMessage (will use the Web.config settings)
smtp.Send(mm)Great Minds must think alike. I just finished this not more than 15
minutes ago. There may be a better way to do it, but I know that this
works.

protected void btnSendUpload_Click(object sender, EventArgs e)
{
string queryString = "Select Email From tblMailer";
string constring = "Data Source=GLSDBS03;Initial
Catalog=CCTS;Persist Security Info=True;User
ID=UserID;Password=Password";
using (SqlConnection connection = new
SqlConnection(constring))
{
connection.Open();

SqlCommand command = new SqlCommand(queryString,
connection);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
string captured = Convert.ToString(reader["EMail"]);
try
{
SmtpClient mailSvr = new SmtpClientYour SMTP
CLient);
MailAddress from = new MailAddress(Your From
Address);
MailAddress to = new MailAddress(captured);
MailMessage msg = new MailMessage(from, to);
msg.Subject = "JACOBS CCTS Labor Upload";
msg.Body = "The attached text file contains Labor
Hours. " + Environment.NewLine + Environment.NewLine;
Attachment attach = new Attachment("\\\\glsdbs03\\d
$\\File\\LaborFile.txt");
msg.Attachments.Add(attach);
mailSvr.Send(msg);
lbStatus.Text = "Labor has been Sent.";
}
catch (Exception ex)
{
lbStatus.Text = ex.Message;
}
}
}
}

I hope this helps

On Nov 21, 4:00 pm, "David C" <dlch...@.lifetimeinc.comwrote:

Quote:

Originally Posted by

I would like to have an email go to 2 email addresses but when I try it by
separating with semicolon or comma it fails. Can anyone help? below is my
code (see ToAddress string). Thanks.
>
David
strUser = UtilClass.GetUserName(strUser)
>
'!!! UPDATE THIS VALUE TO YOUR EMAIL ADDRESS
Const ToAddress As String = "m...@.myemail.com"
Const FromAddress As String = "m...@.myemail.com"
>
'(1) Create the MailMessage instance
Dim mm As New System.Net.Mail.MailMessage(ToAddress,
FromAddress)
>
'(2) Assign the MailMessage's properties
mm.Subject = "Fileroom unhandled exception occurred!"
mm.Body = "User getting error is " & strUser & vbCrLf & _
String.Format("An unhandled exception
occurred:{0}Message: {1}{0}{0} Stack Trace:{0}{2}",
System.Environment.NewLine, ex.Message, ex.StackTrace)
mm.IsBodyHtml = False
>
'(3) Create the SmtpClient object
Dim smtp As New System.Net.Mail.SmtpClient
>
'(4) Send the MailMessage (will use the Web.config settings)
smtp.Send(mm)


in smtp, you write out a header per address, not a comma seperated list. to
support this mailmessage has a To collection, which you add as many addresses
as you want:

var mm = new MailMessage(toAddress1,fromAddress);
mm.To.Add(new MailAddress(toAddress2));

-- bruce (sqlwork.com)

"David C" wrote:

Quote:

Originally Posted by

I would like to have an email go to 2 email addresses but when I try it by
separating with semicolon or comma it fails. Can anyone help? below is my
code (see ToAddress string). Thanks.
>
David
strUser = UtilClass.GetUserName(strUser)
>
'!!! UPDATE THIS VALUE TO YOUR EMAIL ADDRESS
Const ToAddress As String = "me@.myemail.com"
Const FromAddress As String = "me@.myemail.com"
>
'(1) Create the MailMessage instance
Dim mm As New System.Net.Mail.MailMessage(ToAddress,
FromAddress)
>
'(2) Assign the MailMessage's properties
mm.Subject = "Fileroom unhandled exception occurred!"
mm.Body = "User getting error is " & strUser & vbCrLf & _
String.Format("An unhandled exception
occurred:{0}Message: {1}{0}{0} Stack Trace:{0}{2}",
System.Environment.NewLine, ex.Message, ex.StackTrace)
mm.IsBodyHtml = False
>
'(3) Create the SmtpClient object
Dim smtp As New System.Net.Mail.SmtpClient
>
'(4) Send the MailMessage (will use the Web.config settings)
smtp.Send(mm)
>
>
>


"David C" <dlchase@.lifetimeinc.comwrote in message
news:uCJt%23nILIHA.1208@.TK2MSFTNGP03.phx.gbl...

Quote:

Originally Posted by

>I would like to have an email go to 2 email addresses but when I try it by
>separating with semicolon or comma it fails.


Well it would do - it's a collection...
http://www.systemnetmail.com/faq/3.2.3.aspx
--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Perfect. Thanks everyone.

David
"Mark Rae [MVP]" <mark@.markNOSPAMrae.netwrote in message
news:eHV89AJLIHA.3992@.TK2MSFTNGP03.phx.gbl...

Quote:

Originally Posted by

"David C" <dlchase@.lifetimeinc.comwrote in message
news:uCJt%23nILIHA.1208@.TK2MSFTNGP03.phx.gbl...
>

Quote:

Originally Posted by

>>I would like to have an email go to 2 email addresses but when I try it by
>>separating with semicolon or comma it fails.


>
Well it would do - it's a collection...
http://www.systemnetmail.com/faq/3.2.3.aspx
>
>
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

0 comments:

Post a Comment