2011-12-25 13 views
97

Potrei riuscire a consentire all'applicazione Web di inviare e-mail automatiche tramite l'Utilità di pianificazione di Windows. Ora voglio inviare e-mail formattate in HTML utilizzando il seguente metodo che ho scritto per l'invio di e-mail.Come inviare e-mail in formato HTML?

mio code-behind:

protected void Page_Load(object sender, EventArgs e) 
    { 
     SmtpClient sc = new SmtpClient("mail address"); 
     MailMessage msg = null; 

     try 
     { 
      msg = new MailMessage("[email protected]", 
       "[email protected]", "Message from PSSP System", 
       "This email sent by the PSSP system"); 

      sc.Send(msg); 
     } 

     catch (Exception ex) 
     { 
      throw ex; 
     } 

     finally 
     { 
      if (msg != null) 
      { 
       msg.Dispose(); 
      } 
     } 
    } 

come fare? Voglio solo mettere un testo in grassetto con un link e magari un'immagine nell'email.

risposta

158

Impostazione isBodyHtml a true consente di utilizzare tag HTML nel corpo del messaggio:

msg = new MailMessage("[email protected]", 
       "[email protected]", "Message from PSSP System", 
       "This email sent by the PSSP system<br />" + 
       "<b>this is bold text!</b>"); 

msg.IsBodyHtml = true; 
+0

Possiamo usare un carattere personalizzato ?? – Wanderer

+0

Grazie mille @Shai –

+0

Non è possibile personalizzare facilmente i caratteri in quanto i caratteri personalizzati devono essere supportati dal client di posta. Molti client di posta utilizzano ancora un tipo di codice HTML pre IE6 molto elementare, in modo tale che vengano impostati su un altro carattere. –

18

Questo funziona per me

msg.BodyFormat = MailFormat.Html; 

e quindi si può utilizzare HTML nel vostro corpo

msg.Body = "<em>It's great to use HTML in mail!!</em>" 
+7

Funziona per 'MailMessage' in [' System.Web.Mail'] (https://msdn.microsoft.com/en-us/library/system.web.mail.mailmessage.bodyformat%28v=vs.110% 29.aspx). Fare 'IsBodyHtml' è per quello in [' System.Net.Mail'] (https://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.isbodyhtml (v = vs.110) .aspx) –

60

Il modo migliore per inviare e-mail in formato HTML

Questo codice sarà in "Customer.htm"

<table> 
    <tr> 
     <td> 
      Dealer's Company Name 
     </td> 
     <td> 
      : 
     </td> 
     <td> 
      #DealerCompanyName# 
     </td> 
    </tr> 
</table> 

Leggi file HTML Utilizzando System.IO.File.ReadAllText. ottenere tutto il codice HTML nella variabile stringa.

string Body = System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath("EmailTemplates/Customer.htm")); 

Sostituisci stringa particolare al valore personalizzato.

Body = Body.Replace("#DealerCompanyName#", _lstGetDealerRoleAndContactInfoByCompanyIDResult[0].CompanyName); 

chiamata SendEmail (Corpo stringa) Funzione e procedura per l'invio di posta elettronica.

public static void SendEmail(string Body) 
     { 
      MailMessage message = new MailMessage(); 
      message.From = new MailAddress(Session["Email"].Tostring()); 
      message.To.Add(ConfigurationSettings.AppSettings["RequesEmail"].ToString()); 
      message.Subject = "Request from " + SessionFactory.CurrentCompany.CompanyName + " to add a new supplier"; 
      message.IsBodyHtml = true; 
      message.Body = Body; 

      SmtpClient smtpClient = new SmtpClient(); 
      smtpClient.UseDefaultCredentials = true; 

      smtpClient.Host = ConfigurationSettings.AppSettings["SMTP"].ToString(); 
      smtpClient.Port = Convert.ToInt32(ConfigurationSettings.AppSettings["PORT"].ToString()); 
      smtpClient.EnableSsl = true; 
      smtpClient.Credentials = new System.Net.NetworkCredential(ConfigurationSettings.AppSettings["USERNAME"].ToString(), ConfigurationSettings.AppSettings["PASSWORD"].ToString()); 
      smtpClient.Send(message); 
     } 
+3

Forse usare ** MailDefinition ** o ** RazorEngine ** *** http: //stackoverflow.com/questions/10512845/how-to-send-email-wth-email-template-c-sharp *** – Kiquenet

+0

Grazie - questo ha funzionato per me e mi ha salvato ore. –