Sankar M
Sankar M

Reputation: 4809

how to remove [ ] in the mailer

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[[email protected]] i need to remove this set brackets [].

help me... below is my coding.

        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("[email protected]", "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);

Upvotes: 0

Views: 153

Answers (3)

Sankar M
Sankar M

Reputation: 4809

        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("[email protected]");
        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!!!

Upvotes: 0

Jens
Jens

Reputation: 25593

To extract "Shankar" from "Shankar[...]", you could simply use

string address = "Shankar[[email protected]]";
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.

Upvotes: 2

user195488
user195488

Reputation:

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[[email protected]]";

      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

Upvotes: 1

Related Questions