如何删除邮件中的[]

本文关键字:何删除 删除 | 更新日期: 2023-09-27 18:05:46

我使用的是System。网邮政MailMessage thro C#发送电子邮件。问题是,如果发件人的姓名不同,凭据不同,则显示为shankar[admin@mydomain.com]我需要去掉这个括号[]。

帮帮我。。。下面是我的编码。

        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("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);

如何删除邮件中的[]

要从"Shankar[…]"中提取"Shankar",只需使用

string address = "Shankar[admin@contentraven.com]";
string name = address.Substring(0, address.IndexOf('[') - 1);
// here, name contains "Shankar"

如果你正在向用户发送电子邮件,并希望他们的电子邮件客户端不要显示你的地址:这是不可能的。

如果我在跟踪你,那么你可以使用RegEx来提取你需要的字符串,例如:

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();
    }
  }
}

输出:

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);

我们需要在发件人地址中提供有效的EmailId它对我有用!!!