2011-08-17 26 views
0

我使用System.Net.Mail.MailMessage thro C#發送電子郵件。 問題是如果發件人的名稱不同,證書不同,它顯示爲像shankar [[email protected]] 我需要刪除此設置括號[]。如何刪除郵件中的[]

幫我... 以下是我的編碼。

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

你可以嘗試澄清這一點嗎?我沒有跟着你。我們在這裏提供幫助。我已經讀了幾次,我不明白。 – 2011-08-17 12:59:20

+0

當用戶收到一封電子郵件....發件人地址顯示Shankar [[email protected]] ...但我只需要名稱Shankar –

+0

它不取決於電子郵件客戶端如何顯示名稱/地址,至極你沒有任何控制權? – Johnny5

回答

2

要提取 「桑卡」從「Shankar [...]」,你可以簡單地使用

string address = "Shankar[[email protected]]"; 
string name = address.Substring(0, address.IndexOf('[') - 1); 
// here, name contains "Shankar" 

如果您要發送電子郵件給您的用戶,並希望他們的電子郵件客戶端不要顯示您的地址:這不能完成。

+0

+1比我的答案簡單。我只是使用正則表達式來防止郵件程序使用<>而不是[]。 – 2011-08-17 13:21:11

1

如果我跟着你,那麼你可以使用正則表達式來提取您需要的字符串,例如:

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

輸出:

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

我們需要從地址 它的工作原理GUD爲我提供了一個有效的EMAILID!

相關問題