mario.deghetto

Visual Basic & .NET
posts - 332, comments - 79, trackbacks - 0

#16 Inviare e-mail in formato HTML con allegati

Per inviare un’e-mail con allegati e per di più in formato HTML non c’è niente di meglio che una manciata di codice VB.NET, come potete vedere dal seguente codice:

Imports System.Text
Public Class Form1     Dim strBody As _         New StringBuilder("Ciao da Mario De Ghetto!,")     Dim objMail As New System.Net.Mail.MailMessage()     Dim strPath As String = "C:\prova.txt"     Dim strFrom As String = "mittente@dominio.com"     Dim strTo As String = "destinatario@libero.it"     Private Sub Button1_Click(ByVal sender _             As System.Object, _             ByVal e As System.EventArgs) _             Handles Button1.Click         strBody.Append("<br>")         strBody.Append("<br>")         strBody.Append("<strong>Testo di prova</strong>")         strBody.Append("<br>")         strBody.Append("<br>")         strBody.Append("<br>")         strBody.Append("Cordiali saluti.")         strBody.Append("<br>")         strBody.Append("Ditta XYZ")         strBody.Append("<br>")         Dim frmaddress As _             New System.Net.Mail.MailAddress(strFrom)         Dim attachement As _             New System.Net.Mail.Attachment(strPath)         objMail.To.Add(strTo)         objMail.From = frmaddress         objMail.Subject = "Invio documento allegato: " & strPath         objMail.Priority = Net.Mail.MailPriority.High         objMail.IsBodyHtml = True         objMail.Body = strBody.ToString         If strPath <> "" Then             objMail.Attachments.Add(attachement)         End If         Dim SmtMail As _             New System.Net.Mail.SmtpClient("smtp.libero.it")         SmtMail.Send(objMail)         MessageBox.Show("Mail spedita!")     End Sub
End Class  

E’ possibile specificare più di un destinatario, semplicemente mettendo tutti i destinatari nella stessa stringa e separando ciascun indirizzo con una virgola, così:

Dim strTo As String = “destinatario@libero.it, altrodestinatario@tiscali.it”

Il codice è abbastanza chiaro. Ovviamente dovrete personalizzare i dati, come l’indirizzo del vostro server SMTP, l’indirizzo del mittente e del destinatario e così via. Se vi auto-inviate l’e-mail, potrete verificare che effettivamente c’è l’allegato e che il testo è in formato HTML.

Print | posted on mercoledì 14 ottobre 2009 4.57 |

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 8 and 2 and type the answer here:

Powered by: