简体   繁体   中英

Comma before and after an email address of webmail.send

I am trying to implement smtp on asp.net web pages (razor page) and sql server 2017. The email notification was supposed to send after submitting a form using an email address stored in sql database but each I click submit, the notification fails to send. So, I tried debugging it by adding a breakpoint and stepped into the smtp initiallizer. I found out that there is a comma (,) before and after the email address. When I remove the two commas, the notification sends but I don't know how to permanently remove it without having to do it by putting a breakpoint so that my application can run.

Attached is the screenshoot.

Thank you so much for your help.

// Initialize WebMail helper
 WebMail.SmtpServer = "smtp.office365.com";
 WebMail.SmtpPort = 25;
 WebMail.UserName = "help@me.please";
 WebMail.Password = "Help123";
 WebMail.From = "help@me.please";
 WebMail.EnableSsl = true;



 WebMail.Send(to: Email,
 subject: "Visitor Alert",
 body: " Hello: <br/> " + "Hope you're having a good time!);

在此处输入图片说明

It seems there are invalid entries in your database, you should validate that because everybody might be trying to put in corrupted data. Another possibility is that some parsing went wrong which resulted in corrupted data.

Anyhow, to fix this specific case, you can trim the email address:

WebMail.Send(to: email.Trim(','),
             subject: "Visitor Alert",
             body: " Hello: <br/> " + "Hope you're having a good time!); 

Nevertheless, this is just fixing a symptom for another underlying problem.

What would work is the following:

// Initialize WebMail helper
WebMail.SmtpServer = "smtp.office365.com";
WebMail.SmtpPort = 25;
WebMail.UserName = "help@me.please";
WebMail.Password = "Help123";
WebMail.From = "help@me.please";
WebMail.EnableSsl = true;



WebMail.Send(to: Email.Replace(',',''),
subject: "Visitor Alert",
body: " Hello: <br/> " + "Hope you're having a good time!)";

I added a

.Replace(',','')

which removes the unwanted commas from any email you have

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM