i'm using System.Net.Mail.MailMessage thro C# to send an email. the issue is if sender's name is different and credential is diferent it shows like shankar[admin@mydomain.com] i need to remove this set brackets [].
help me... below is my coding.
System.Net.Mail.MailM开发者_运维知识库essage oMail = new System.Net.Mail.MailMessage();
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
oMail.From = new System.Net.Mail.MailAddress("admin@mydomain.com", "shankar123");
oMail.To.Add(TextBox1.Text.Trim());
oMail.Subject = "Subject*";
oMail.Body = "Body*";
oMail.IsBodyHtml = true;
smtp.Host = "smtp.sendgrid.net";
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("myusername", "mypassword");
smtp.UseDefaultCredentials = false;
smtp.Credentials = cred;
smtp.Send(oMail);
To extract "Shankar" from "Shankar[...]", you could simply use
string address = "Shankar[admin@contentraven.com]";
string name = address.Substring(0, address.IndexOf('[') - 1);
// here, name contains "Shankar"
If you are sending emails to your users, and wish their email client not to show your address: This can't be done.
If I am following you, then you can use a RegEx to extract the string you need, for example:
using System;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string txt="Shankar[admin@contentraven.com]";
string re1="((?:[a-z][a-z0-9_]*))"; // Variable Name 1
Regex r = new Regex(re1,RegexOptions.IgnoreCase|RegexOptions.Singleline);
Match m = r.Match(txt);
if (m.Success)
{
String var1=m.Groups[1].ToString();
Console.Write(var1.ToString()+"\n");
}
Console.ReadLine();
}
}
}
Output:
Shankar
System.Net.Mail.MailMessage oMail = new System.Net.Mail.MailMessage();
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
oMail.From = new System.Net.Mail.MailAddress("one@gmail.com");
oMail.To.Add(TextBox1.Text.Trim());
oMail.Subject = "Subject*";
oMail.Body = "Body*";
oMail.IsBodyHtml = true;
smtp.Host = "smtp.sendgrid.net";
System.Net.NetworkCredential cred = new System.Net.NetworkCredential cred = new System.Net.NetworkCredential("myusername", "mypassword");
smtp.UseDefaultCredentials = false;
smtp.Credentials = cred;
smtp.Send(oMail);
we need to provide a valid EmailId in From address it works gud for me!!!
精彩评论