I'm using MailAddress to attempt to validate a given email.
I get a valid email when I give it a string like "someName#gmail".
I was expecting it to fail with a FormatException since the email is missing .com at the end. Why is it not failing to parse the email here?
public bool IsEmailValid(string emailaddress)
{
try
{
MailAddress m = new MailAddress(emailaddress);
return true;
}
catch (FormatException)
{
return false;
}
}
x#host is a legally formatted email address. The MailAddress class only looks at the format of the string, not the validity of the address. You'll have to resolve the host name, send an email to the recipient and wait for the absence of a bounce mail or the presence of a return receipt to fully validate the address.
someName#gmail is actually a perfectly valid email address, in that it conforms to the email format specification (formally IETF RFC2822). However it is almost certainly not routable unless you happen to have a mail server on your local network with the DNS name gmail.
If you would like to check for this particular problem, I would suggest adding a second check that there is a dot after the # symbol.
EDIT: Please note that this is a business decision that you have to make. By invalidating email addresses with a . after the #, you are breaking the standard email specification, however I feel that in perhaps 99.99% of cases dealing with external mail this is an acceptable tradeoff to make to prevent users mistakenly typing myname#hotmail or myname#gmail (I'd wager a common mistake among new Internet users).
This could conceivably block legitimate external email, say if I owned the TLD tt I could have a legitimate email address bob#tt, and mail handling systems like yours should handle my address without complaining. However as it has been noted in this answer, this is very uncommon.
Having said that, here's some code that would achieve this:
public bool IsEmailValid(string emailaddress){
if (emailaddress == null || !emailaddress.Contains("#") || !emailaddress.Split("#")[1].Contains("."))
{
return false; // Doesn't contain "#" or doesn't contain a "." after the "#"
}
try
{
MailAddress m = new MailAddress(emailaddress);
return true;
}
catch (FormatException)
{
return false;
}
}
Unless you have a very limited range of possible email addresses, don't go trying to limit top level domains. For example only allowing .com, .net and .org would be a big mistake - as of writing there are over 1500 TLDs which an email address could conceivably end in
Related
I am developing a software which send email after the user pressed a button.
I encounter some issues with these emails: they sometimes raise this exception: The specified string is not in the form required for an e-mail address.. I emphasise on sometimes because they actually works sometimes.
Surprising thing is that the mail addresses are actually correctly formatted: they all look like blahblah#domain.com or Blah Blah <blahblah#domain.com>. This even happens with some of my company's mails, which - of course - are valid. I even checked all the addresses with this regex and they all passed: ^\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*$.
Here are the steps my software is going through:
Add the main address:
var mail = new MailMessage(new MailAddress("logistics#company.com", "company logistics department"), new MailAddress(address));
Here the address is valid since the software did send an email to this address prior to this step.
In a loop I add the CCs addresses:
mail.CC.Add(new MailAddress(email));
Here I applied a null/empty check so I am not adding any empty mail address.
And finally I add myself in BCC address:
mail.Bcc.Add("my#company.com");
Is there any issue in the way I'm doing this ?
Are there some known issues (additionnaly random ones) about the C# .NET mail class?
Is there a limit on the maximum amount of To / CC / Bcc addresses this class can handle ?
EDIT: i'm using System.Net.Mail classes
Sorry if I failed to find an existing post with my problem.
What am I trying to do is as follows:
I simply want to send a couple of emails (2-3) to different people and more importantly with different content. The emails are official, important and also logged in the system. Long story short, I need when for some reason one of them fails to stop the sending of the others. I need either all of them sent or none of them.
What have I done so far
It is not the first time the system I worked on has to send an automatic email. The application is an ASP MVC website. So some time ago, I installed the MvcMailer (MvcMailer) and used it the way it was explained. It worked quite well and so I liked the idea of previewing the email (as you can give it a view to send).
So, in the light of my new problem I read carefully the MvcMailer documentation and did not find anything about sending multiple emails in transaction-like manner. Couldn't think of a way to force them to behave in this way. In my tests when I send an email, even if it is one email with a few CCs, all working mails get send and just one of them fails (wrong email name, closed port ... whatever).
I was hoping someone could suggest me a way to achieve something like this. I hope my explanation was sufficient and if not, let me know I will provide you with all details required.
If this is impossible with the MvcMailer, then is it possible with other tools ?
My implementation so far: (keep in mind I'm still in the testing stages)
public class MailerController : Controller
{
private MailerRepository mailerRep;
public MailerController()
{
this.mailerRep = new MailerRepository();
}
public void TestTransmittalEmail()
{
var model = this.mailerRep.GetTransmittalModel(1234); //I've stucked a specific clientId
var mailer = new TransmittalsMailer();
mailer.TransmittalEmail(model).Send();
}
}
public class TransmittalsMailer : MailerBase
{
public TransmittalsMailer()
{
}
public MvcMailMessage TransmittalEmail(TransmittalManifestModel model)
{
var mailMessage = new MvcMailMessage() { Subject = "Transmittals - TESTING EMAIL" };
//embedding a few images in the email
var resources = new Dictionary<string, string>();
resources["logo"] = PDFResourcePaths.VripackLogo;
resources["companyInfo"] = PDFResourcePaths.CompanyInfoBlock;
mailMessage.To.Add("test1#email.com");
mailMessage.Attachments.Add(new Attachment(#"D:\ASD\TransmittalFolders\1\Archives\150812.1433.MMA.rar"));
ViewData["model"] = model;
this.PopulateBody(mailMessage, "TransmittalEmailView", resources);
return mailMessage;
}
}
This is actually quite a difficult problem to solve. This is because when you send an email, it will work as long the SMTP server can be found (no exception will be thrown).
So you basically have to wait some arbitrary amount of time, and check the inbox of the email you sent it from for the delivery failure. If there is a delivery failure, you stop sending.
So long story short, you shouldn't probably do this. You should simply send all three and notify yourself some other way (probably another email) that there was a failure and which email failed.
You can check if an exception is thrown and then cancel the sending of subsequent emails until the issue is addressed. However this only deals with issues on your end on the sending of each email individual email. If the first two mails are successfully sent, and the last throws an exception, you can't unsend the first emails.
A possible workaround (ugly as it is) would be to initially send emails to an email address you control. If an exception is thrown at this step, log the error, and do not send any further emails until the error is dealt with. If no exceptions are raised, send the emails. However this does not and cannot handle issues that may occur on the recipients side.
A delivery failure notice is not guaranteed as depending on the configuration of your SMTP and the recipient SMTP a delivery failure notification might not be sent. Even if delivery failure notifications are enabled, the notification might not be sent for days (in one of my previous jobs, email delivery failure notifications were not sent until 14 days had elapsed).
Ideally, this should be dealt with at the initial input of the email addresses before you ever send any documents to anyone. You simply send a verification email to the email address in question and have the user clicking on a verification link back to a web service you control. Until this has been done, you don't send any emails to the intended recipient.
I have a .NET 4.0 application which sends email with the SmtpClient.Send() method. Sometimes our SMTP server will return an error like "550 Unknown user", so I wrote a handler which catches SmtpFailedRecipientsException and looks at the FailedRecipient property of each of the SmtpFailedRecipientException objects within SmtpFailedRecipientsException.InnerExceptions, to determine if the failure was because of a To address, a CC address, or a Bcc address. (We want to handle BCC failures separately.)
The FailedRecipient property is documented to be "A String that contains the e-mail address." However, I've found that my attempts to determine which kind of recipient failed is failing because FailedRecipient is including angle brackets, so it looks like "<failedrecipient#domain.com>" instead of "failedrecipient#domain.com"! Is it correct that that property should include angle brackets, so that all consumers of that property have to check to see if they're there and then strip them off before it's useful?
EDIT
Ugh... okay, if referencesource.microsoft.com is to be believed, and the code hasn't changed from 4.0 to 4.5.1, then I think I've found what's causing the problem. The SmtpClient.Send() method calls SmtpTransport.SendMail() to communicate with the SMTP server. Since the SMTP protocol requires that the RCPT TO: command have its email address parameter delimited with angle brackets, SmtpTransport.SendMail() calls the internal method MailAddress.GetSmtpAddress() to get the value to send to the SMTP server:
internal string GetSmtpAddress(bool allowUnicode)
{
return String.Format(CultureInfo.InvariantCulture, "<{0}>", GetAddress(allowUnicode));
}
But if the RCPT TO: command fails, then SmtpTransport.SendMail() populates the SmtpFailedRecipientException with this calculated "SMTP address", instead of the MailAddress.Address property!
Is this a bug-report-worthy problem?
EDIT Dec 15 2014
I opened Connect bug 970353 three months ago, and gave them code to reproduce the issue. No word yet.
Mail servers often present email addresses in this format to clients: "John Smith" <john.smith#domain.com>.
You should expect addresses of this format to maybe come back. You could try passing the string you receive to the System.Net.Mail.MailAddress class to parse it for you:
string emailAddress = new System.Net.Mail.MailAddress("\"John Smith\" <john.smith#domain.com>").Address;
There's further information on this class here.
I am trying to send email to multiple recipients and it works fine when all the recipients have valid email address.
But when one of the recipients have invalid email address, email is not sent even to other recipients whose email address is valid and I am getting an exception:
The server rejected one or more recipient addresses. The server
response was: 550 #5.1.0 Address rejected.
Is there any way I can send the email to other valid recipients even if one of the email address is invalid?
public static void sendMails(string ptxtSubject, string ptxtBody)
{
string txtTo = "valid1#aaa.com,valid2#aaa.com,invalid1#aaa.com";
string txtFrom = "valid#aaa.com";
string txtSubject = ptxtSubject;
string txtBody = ptxtBody;
MailMessage mail = new MailMessage();
mail.To = txtTo;
mail.From = txtFrom;
mail.Subject = txtSubject;
mail.Body = txtBody;
try
{
SmtpMail.SmtpServer ="smtp.aaa.com";
SmtpMail.Send(mail);
}
catch (Exception ex)
{
//log the exception
throw;
}
}
I can send separate mail to each of them but users(recipients) will not know who else is in the email distribution list. My requirement is everyone should be able to know who else is receiving the email.
Outlook sends the email to all the valid users and notifies us back of invalid users. Is there anyway we can do the same using C#?
Unless all the recipients definitely know each other (and they don't mind other people knowing they are receiving email from you), you should be sending separate emails anyway.
This would also take care of your problem, i.e. if one send operation fails, it won't disrupt the others. Note that in your case, it appears that the initial relay is failing because the addresses are from the same host as the SMTP server.
Once an email is routed to multiple hosts, the success/failure is no longer interdependent. For example, a gmail.com server probably doesn't know/care that a yahoo.com server rejected a recipient.
If performance is a concern, you can send the messages asynchronously to get achieve better throughput. Note that you can still handle exceptions when sending asynchronously.
As always, if you are sending any quantity of email, it's probably advisable to use a 3rd party service.
I really don't see any way to accomplish this. Sending an email with SmtpMail.Send() is pretty much an atomic function and you need correct data for it to work without an exception.
The only option I see here is to send separate emails to each recipient.
I have created a community portal, in which user creates his/her account. After successfull registration a confirmation mail is send on registered email address.
I am using the following code to send the mail -
private void SendMail(string recvr, string recvrName, string verCode, int NewUserID)
{
try
{
string emailID = ConfigurationManager.AppSettings["WebMasterMail"];
string mailPass = ConfigurationManager.AppSettings["pass"];
string mailer = ConfigurationManager.AppSettings["mailer"];
MailMessage msg = new MailMessage();
MailAddress addrFrom = new MailAddress(emailID, "Panbeli.in.... Bari community portal");
MailAddress addrTo = new MailAddress(recvr, recvrName);
msg.To.Add(addrTo);
msg.From = addrFrom;
msg.Subject = "You have registered sucessfully on PanBeli.in.";
msg.Priority = MailPriority.High;
msg.Body = RegisterMessageBody(recvrName, verCode,NewUserID);
msg.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient(mailer);
smtp.Credentials = new System.Net.NetworkCredential(emailID, mailPass);
smtp.Send(msg);
}
catch (Exception Ex) { }
}
While testing we found that all the confirmation mails are going to SPAM folder instead of Inbox.
Is there anything wrong with the code or is there anything related to security.
Can anybody suggest solution to this problem.
Thanks for sharing your time.
It sounds like your email is getting flagged by SpamAssassin or the like, so you just need to focus on changing your email enough to not get flagged.
Your content doesn't sound like it has any reason to rate high for the Bayesian score, so I don't think thats the problem. It wouldn't hurt to try removing possible trigger words though.
Your message is marked with high priority. Do you need this? This just adds into one of the scoring metrics in a spam filter. Spam is often marked with high priority, so your message will be treated with more scrutiny. On the otherhand, for some filters marking your message with high priority will mean less scrutiny.
IsBodyHTML is marked true, but you're only providing text/html. You minimally need to include an alternate view with text/plain.
message.IsBodyHtml = true;
string html = RegisterMessageBodyHtml(recvrName, verCode,NewUserID);
string plain = RegisterMessageBodyPlaintext(recvrName, verCode, NewUserID);
message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, new ContentType("text/html"));
message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(plain, new ContentType("text/plain"));
See how Google treats your message. In gmail, open a test message that you've sent, click the downfacing arrow next to the reply button, and select "Show Original". You'll see how Google treated your message. Look for headers like:
Received-SPF: softfail (google.com: domain of transitioning xxx#xxx.org does not designate xx.xx.xx.xx as permitted sender) client-ip=xx.xx.xx.xx;
Authentication-Results: mx.google.com; spf=softfail (google.com: domain of transitioning xxx#xxx.org does not designate xx.xx.xx.xx as permitted sender)
Read up on the default rule set for SpamAssassin as it will probably be a good reference on the rule sets for most filters. If you can identify why your message is getting flagged, you can fix it.
Emails Marked as Spam
This is not a programming issue unfortunately, but I can understand why you might think it is. The code is sending the emails, and they have been sent as you reported. So this is highly unlikely to be a problem with your code, because it's served it's purpose fully!
Getting around it
It all comes down to the recipients mail client (the software they are using to view the emails with), or the services that process the emails at some sort of gateway, or a combination of both of these!
All of these elements have vastly varied algorithms and metrics for determining if an email is probably spam or not. So a one fit all solution is sadly not possible. Some are intelligent, other less so, some brutally discard a huge % of emails, others operate purely on a 'not on white list, you're not getting in' policy, and then there are those that just let everything come in regardless of content/origin.
The ways to go around fixing this are:
To try and get on white lists for major email providers.
Educate your audience to add the senders email address as a trusted contact.
Check your mail server IP isn't blacklisted by some providers. It's possible your IP address was previously used to send spam.
Experiment with the emails content
Your from address is invalid. Try putting in a real email address that points to a valid mailbox. Preferably this email address is on the same domain as the SMTP server you use to send the mail with. If not, read into SPF http://en.wikipedia.org/wiki/Sender_Policy_Framework
This happen to me to and it's solved now,
I just set the BodyEncoding and SubjectEncoding proprieties on the MailMessage object,
and added the DOCTYPE and the html tags to my email header,
var msg = new MailMessage
{
Subject = subject,
Body = body,
BodyEncoding = System.Text.Encoding.UTF8,
SubjectEncoding = System.Text.Encoding.Default,
IsBodyHtml = true
};
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
It's working perfectly now
Add following line in your code while creating MailMessage
msg.BodyEncoding = System.Text.Encoding.GetEncoding("utf-8");
This happens a lot even for the house hold names. I sympathise with you as you only want a plain text email but if your clients really want those emails and you really want your logo in then they could just white list your domain so that all emails get through!
We use a company called mailchimp for sending out subscriber mails and I questioned them on how to avoid spam filters especially in the context of essentially an advert out to a large group it can be very difficult to avoid them, here is their advice and there is lots of it.;
Avoiding the Spam Filters
For anyone having this problem, it looks like Google mark as spam any mails using the default ASP.NET e-mail authentication subject and body. I.e.:
"Please confirm your account by clicking here."
Changing the text allows the e-mail to pass the spam filter