I am only capturing all valid email addresses from email body using below method.
public static IEnumerable<string> ParseAllEmailAddressess(string data)
{
HashSet<String> emailAddressess = new HashSet<string>();
Regex emailRegex = new Regex(#"\w+([-+.]\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*", RegexOptions.IgnoreCase);
MatchCollection emailMatches = emailRegex.Matches(data);
foreach (Match emailMatch in emailMatches)
{
emailAddressess.Add(emailMatch.Value);
}
return emailAddressess;
}
The problem here is outlook converts the Signature image into some random email address something like (image001.png#01D36870.C9EE4D60) . And my method considering it as valid email address and captures it. I want to strip off such email address while parsing email body.
I can think of splitting the email address with . before # site and use the first index to match the image extension ".png" to identify valid email or not. But i think it not very efficient. Applying some reg ex to strip signature images content would be fast.
Any help would be appreciate.
I end up creating below method to strip the signature images email address from the email body.
public static readonly string[] _validExtensions = { "jpg", "bmp", "gif", "png", "jpeg","tiff","raw","psd" };
public static bool IsImageExtension(string email)
{
bool isContainsImageExt = false;
MailAddress addr = new MailAddress(email);
string username = addr.User;
if (!string.IsNullOrEmpty(username) && username.Contains('.'))
{
String[] parts = username.Split(new[] { '.' });
if(!string.IsNullOrEmpty(parts[0]) && !string.IsNullOrEmpty(parts[1]))
{
if(_validExtensions.Contains(parts[1].ToLower()) && (parts[0].ToLower().Contains("image")))
{
isContainsImageExt = true;
}
}
}
return isContainsImageExt;
}
Related
I'm trying to extract en email with the + special character but for some reason the ParseQueryString skips it:
namespace ParsingProblem
{
class Program
{
static void Main(string[] args)
{
var uri = new System.Uri("callback://gmailauth/#email=mypersonalemail15+1#gmail.com");
var parsed = System.Web.HttpUtility.ParseQueryString(uri.Fragment);
var email = parsed["#email"];
// Email is: mypersonalemail15 1#gmail.com and it should be mypersonalemail15+1#gmail.com
}
}
}
The + symbol in a URL is interpreted as a space character. To fix that, you need to URL encode the email address first. For example:
var urlEncodedEmail = System.Web.HttpUtility.UrlEncode("mypersonalemail15+1#gmail.com");
var uri = new System.Uri($"callback://gmailauth/#email={urlEncodedEmail}");
var parsed = System.Web.HttpUtility.ParseQueryString(uri.Fragment);
var email = parsed["#email"];
I don't have much knowledge of regex, so please if you can help me, how to match this string:
a href="mailto:bristovski#moznosti.com.mk",
which is substring of other string.
Thanks.
Instead of using a regular expression to validate an email address, you can use the System.Net.Mail.MailAddress class. To determine whether an email address is valid, pass the email address to the MailAddress.MailAddress(String) class constructor.
public bool IsValid(string emailaddress)
{
try
{
MailAddress m = new MailAddress(emailaddress);
return true;
}
catch (FormatException)
{
return false;
}
}
Or
string email = txtemail.Text;
Regex regex = new Regex(#"^([\w\.\-]+)#([\w\-]+)((\.(\w){2,3})+)$");
Match match = regex.Match(emailaddress);
if (match.Success)
Response.Write(emailaddress + " is correct");
else
Response.Write(email + " is incorrect");
I have tried this code to validate multiple email addresses:
string email = "kamilar#recruit12.com; test#minh.com; test2#yahoo.com";
REGEX_EMAIL_ADDRESS_MULTI = #"^\s*([a-zA-Z0-9_%+~=$&*!#?\-\'](\.)?)*[a-zA-Z0-9_%+~=$&*!#?\-\']#([a-zA-Z0-9-](\.)?)+[a-zA-Z]{2,6}(\.[a-zA-Z]{2,6})+\s*((,|;)\s*([a-zA-Z0-9_%+~=$&*!#?\-\'](\.)?)*[a-zA-Z0-9_%+~=$&*!#?\-\']#([a-zA-Z0-9-](\.)?)+[a-zA-Z]{2,6}(\.[a-zA-Z]{2,6})+\s*)*$";
Regex reg = new Regex(REGEX_EMAIL_ADDRESS_MULTI);
var isOk = reg.IsMatch(email);
But it does not match - why?
Note that it matches with single address with this following expression:
#"^\s*([a-zA-Z0-9_%\-\'](\.)?)*[a-zA-Z0-9_%\-\']#([a-zA-Z0-9-](\.)?)+[a-zA-Z]{2,6}(\.[a-zA-Z]{2,6})+\s*$"
Any help?
UPDATED:
I do NOT want to split the string to validate one by one! That's why I need to ask on Stack Overflow!
As others have noted, you should be validating them one at a time.
string email = "kamilar#recruit12.com; test#minh.com; test2#yahoo.com";
string[] emailAddresses = email.Split(';').Select(x=>x.Trim()).ToArray();
string REGEX_EMAIL_ADDRESS_MULTI = #"^\s*([a-zA-Z0-9_%+~=$&*!#?\-\'](\.)?)*[a-zA-Z0-9_%+~=$&*!#?\-\']#([a-zA-Z0-9-](\.)?)+[a-zA-Z]{2,6}(\.[a-zA-Z]{2,6})+\s*((,|;)\s*([a-zA-Z0-9_%+~=$&*!#?\-\'](\.)?)*[a-zA-Z0-9_%+~=$&*!#?\-\']#([a-zA-Z0-9-](\.)?)+[a-zA-Z]{2,6}(\.[a-zA-Z]{2,6})+\s*)*$";
bool isOk = true;
foreach (string emailAddress in emailAddresses)
{
Regex reg = new Regex(REGEX_EMAIL_ADDRESS_MULTI);
if (!reg.IsMatch(email))
{
isOk = false;
break;
}
}
split the string at the ';'
string email = "kamilar#recruit12.com; test#minh.com; test2#yahoo.com";
string[] emails = email.Split(';');
then create a method that returns the validity
private bool CheckAddress(string address){
REGEX_EMAIL_ADDRESS_MULTI = #"^\s*([a-zA-Z0-9_%+~=$&*!#?\-\'](\.)?)*[a-zA-Z0-9_%+~=$&*!#?\-\']#([a-zA-Z0-9-](\.)?)+[a-zA-Z]{2,6}(\.[a-zA-Z]{2,6})+\s*((,|;)\s*([a-zA-Z0-9_%+~=$&*!#?\-\'](\.)?)*[a-zA-Z0-9_%+~=$&*!#?\-\']#([a-zA-Z0-9-](\.)?)+[a-zA-Z]{2,6}(\.[a-zA-Z]{2,6})+\s*)*$";
Regex reg = new Regex(REGEX_EMAIL_ADDRESS_MULTI);
return reg.IsMatch(email);
}
now just loop through the addresses
for(int i = 0; i > emails.Length; i++){
var isOK = CheckAddress(emails[i]);
}
This address is bad an invalidates the address string
These addresses are OK and the string is allowed
It is my fault as the first email address does not passed the single email regex test so the multiple email regex test should fail.
Thanks.
I need to get the host from an email address string.
In .net 4.x I did this
var email1 = "test#test.com";
var email2 = "test2#yea.test.com"
var email1Host = new MailAddress(email1).Host;
var email2Host = new MailAddress(email2).Host;
email1Host prints "test.com"
email2Host prints "yea.test.com"
But now i need only the "test.com" part in both examples.
.Net Standard library 1.6 doesnt have the System.Net.Mail class so I can't do this anymore.
Whats another way of accomplishing the same thing in .net core but I only need the test.com part
I know there is a System.Net.Mail-netcore nuget package, but I really want to avoid installing a nuget just for this
Edit: Sorry for the confusion I forgot to mention that I only need the test.com
More examples were requested
#subdomain1.domain.co.uk => domain.co.uk
#subdomain1.subdomain2.domain.co.uk => domain.co.uk
#subdomain1.subdomain2.domain.com => domain.com
#domain.co.uk => domain.co.uk
#domain.com => domain.com
Using String Split and Regex,
var email1 = "test#test.com";
var email2 = "test2#yea.test.co.uk";
var email1Host = email1.Split('#')[1];
var email2Host = email2.Split('#')[1];
Regex regex = new Regex(#"[^.]*\.[^.]{2,3}(?:\.[^.]{2,3})?$");
Match match = regex.Match(email1Host);
if (match.Success)
{
Console.WriteLine("Email Host1: "+match.Value);
}
match = regex.Match(email2Host);
if (match.Success)
{
Console.WriteLine("Email Host2: "+match.Value);
}
Update: Using regex to get the Domain name
An alternative is to use the System.Uri class and prefix the email with 'mailto'.
class Program
{
static void Main(string[] args)
{
string email = "test#test.com";
string emailTwo = "test2#subdomain.host.com";
Uri uri = new Uri($"mailto:{email}");
Uri uriTwo = new Uri($"mailto:{emailTwo}");
string emailOneHost = uri.Host;
string emailTwoHost = uriTwo.Host;
Console.WriteLine(emailOneHost); // test.com
Console.WriteLine(emailTwoHost); // subdomain.host.com
Console.ReadKey();
}
}
Well, a bit of C# should do the trick:
string email = "test#test.com";
int indexOfAt = email.IndexOf('#');
//You do need to check the index is within the string
if (indexOfAt >= 0 && indexOfAt < email.Length - 1)
{
string host = email.Substring(indexOfAt + 1);
}
I have an ASP.Net web application that's been tested extensively. One portion of it sends an email automatically using System.Net.Mail.SmtpClient. During testing, from various Windows 7 machines, the emails were sent and were formatted with a reasonable font size. I don't set the font size in the code, BTW. Now that the application's in production, the first email got sent in a very small font and the users want the font size increased. I can hard-code the font size of course, but I'd prefer to understand what's going on here. What determines the font size? Is it something in the SmtpClient, or some setting on the SMTP server, or what? Below is the code that sends the email. TemplateEditor is an AjaxControlToolkit.HTMLEditor control on the page. AttyParaMessageBody is the variable that contains the email body. Our users are on Windows 7 with Outlook 2010.
string AttyParaMessageBody = TemplateEditor.Content;
LHEmail.SendEmail(LHClassLibrary.LHConfigurationManager.AppSettings["ApprovedNewVersionLHEmailSubject"].ToString(), AttyParaMessageBody, AttyParaAddressees, CurrentLitHoldDetails.ResponsibleAttorney.Email, LHClassLibrary.LHConfigurationManager.AppSettings["EmailCCList"].ToString() + ";" + tbEmails.Text);
public static void SendEmail(string subject, string body, string to, string from, string cc)
{
SendEmail(subject, body, to, from, cc, "", "", MailPriority.High);
}
public static void SendEmail(string subject, string body, string to, string from, string cc, string bcc, string fileName, MailPriority Priority)
{
MailMessage msgMail = new MailMessage();
SmtpClient emailClient = new SmtpClient();
try
{
msgMail = BuildMessage(subject, body, to, cc, bcc, from, fileName, Priority);
emailClient.Send(msgMail);
}
catch (Exception ex)
{
string exception = ex.ToString();
}
finally
{
msgMail.Dispose();
}
}
private static MailMessage BuildMessage(string subject, string body, string to, string cc, string bcc, string from, string fileName, MailPriority Priority)
{
MailMessage msgMail = new MailMessage();
if (!to.Equals(string.Empty))
{
//format emails for .NET 4.0 version
string[] toAddressList = to.Split(';');
//Loads the To address field
foreach (string toaddress in toAddressList)
{
if (toaddress.Length > 0)
{
msgMail.To.Add(toaddress);
}
}
//optional args
//format emails for .NET 4.0 version
if (!cc.Equals(string.Empty))
{
string[] ccAddressList = cc.Split(';');
//Loads the Cc address field
foreach (string ccaddress in ccAddressList)
{
if (ccaddress.Length > 0)
{
msgMail.CC.Add(ccaddress);
}
}
}
if (!bcc.Equals(string.Empty))
{
string[] bccAddressList = bcc.Split(';');
//Loads the Bcc address field
foreach (string bccaddress in bccAddressList)
{
if (bccaddress.Length > 0)
{
msgMail.Bcc.Add(bccaddress);
}
}
}
if (!fileName.Equals(string.Empty))
msgMail.Attachments.Add(new Attachment(fileName));
msgMail.Priority = Priority;
msgMail.From = ((from == null) || (from.Equals(string.Empty))) ? new MailAddress("LitHold#kramerlevin.com", "Litigation Hold") : new MailAddress(from, "Litigation Hold");
msgMail.Subject = subject;
msgMail.Body = body;
msgMail.IsBodyHtml = true;
}
else { throw new SmtpFailedRecipientException("Failed to provide destination address"); }
return msgMail;
}
From my experience, when i need to format my e-mail content that will be sent through SMTP, i will use HTML tag when i set value for the content variable. For example:
String content = "<h2>Thank you for your e-mail</h2><font size=4>We appreciate your feedback.<br/> We will process your request soon</font><br/>Regards.";
If ur content will be set in the code behind, then u can using the above method.
Hope it helps.