[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 247
  • Last Modified:

how to send email to multipile reciepients using using System.Net.Mail;

I can send to one reciepients but fails for multiple recippients


public static void SendEmail(string tomail, string subject, string body, bool HtmlFormat, string frommail = "no-replies@TitleTec.com", string attachfilepath = "")
        {
            MailMessage Message = default(MailMessage);
            Attachment Attach = null;
            MailAddress FromAddress = default(MailAddress);
            MailAddress ToAddress = default(MailAddress);
            //SmtpClient SmtpMail = new SmtpClient(TRASCommon.NotEmpty(ConfigurationManager.AppSettings("SmtpServer"), "localhost"));
            SmtpClient SmtpMail = new SmtpClient(ConfigurationManager.AppSettings["SmtpServer"]);
                            if (!string.IsNullOrEmpty(attachfilepath))
                {
                    Attach = new Attachment(attachfilepath);
                }
                FromAddress = new MailAddress(frommail);
                ToAddress   = new MailAddress(tomail);
                Message     = new MailMessage();

                {
                    Message.To.Add(tomail);
                    Message.From = FromAddress;
                    Message.Subject = subject;
                    Message.Body = body;
                    if (HtmlFormat)
                        Message.IsBodyHtml = true;
                    if (FileExists(attachfilepath))
                        Message.Attachments.Add(Attach);
                }

                SmtpMail.Send(Message);

            }
0
Meinhoonaa
Asked:
Meinhoonaa
  • 3
1 Solution
 
PsyberionCommented:
Can you confirm that tomail contains the email addresses you expect? Have you stepped into the function and checked it's value just to be sure?

The Add method also takes a MailAddress object, so you could try...

Message.To.Add(new MailAddress(tomail));

Open in new window

0
 
PsyberionCommented:
By the way, I should mention that my alternative won't work if tomail contains more than one address. Each address would have to be added with separate statements for each address. So...

Message.To.Add(new MailAddress("email1@domain.com"));
Message.To.Add(new MailAddress("email2@domain.com"));
Message.To.Add(new MailAddress("email3@domain.com"));

Open in new window

Can you post the value of tomail? If it has multiple email addresses they should be separated by commas.
0
 
MeinhoonaaAuthor Commented:
tomail="xx@yy.com,x1@y.com,x2@y2.com"
0
 
PsyberionCommented:
Right, so when you pass "xx@yy.com" as tomail it works, but if you pass "xx@yy.com,x1@y.com,x2@y2.com" it doesn't?

Do you get any kind of error message or exception? Or does the email just not arrive at the mailboxes?

There does appear to be some redundant code, and it looks like some bits are missing as the last section in curly braces has nothing before it like an 'if' or something that would require them. For example the first Message object is never used 'MailMessage Message = default(MailMessage)' as it's overwritten with 'Message = new MailMessage();' later on. HtmlFormat will be true or false as it is a boolean type, the IsBodyHtml property of the message is also a boolean, so there is no need to check the value just assign HtmlFormat straight to the IsBodyHtml property.

Here's a more streamlined version, it's not tested, but give it a try...

public static void SendEmail(string tomail, string subject, string body, bool HtmlFormat, string frommail = "no-replies@TitleTec.com", string attachfilepath = "")
{
	MailMessage Message = new MailMessage(frommail, tomail, subject, body);
	Message.IsBodyHtml = HtmlFormat;
	if (!string.IsNullOrEmpty(attachfilepath))
	{
		Message.Attachments.Add(new Attachment(attachfilepath));
	}
	SmtpClient SmtpMail = new SmtpClient(ConfigurationManager.AppSettings["SmtpServer"]);
	SmtpMail.Send(Message);
}

Open in new window

HTH.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now