The Dark Side of .NET Programming

Il blog di Michele Aponte
posts - 184, comments - 273, trackbacks - 22

Mandare una mail ad una cartella con Asp.Net

Come molti sanno mandare una mail con asp.net è un’operazione abbastanza semplice, basta aggiungere al web.config:

<system.net>
    <mailSettings>
        <smtp from="test@foo.com">
            <network host="smtpserver1" port="25" userName="username" password="secret" />
        </smtp>
    </mailSettings>
</system.net>

Mentre nel codice basta scrivere:

   1:  MailMessage message = new MailMessage();
   2:  message.From = new MailAddress("sender@foo.bar.com");
   3:   
   4:  message.To.Add(new MailAddress("recipient1@foo.bar.com"));
   5:  message.To.Add(new MailAddress("recipient2@foo.bar.com"));
   6:  message.To.Add(new MailAddress("recipient3@foo.bar.com"));
   7:   
   8:  message.CC.Add(new MailAddress("carboncopy@foo.bar.com"));
   9:  message.Subject = "This is my subject";
  10:  message.Body = "This is the content";
  11:   
  12:  SmtpClient client = new SmtpClient();
  13:  client.Send(message);

Durante lo sviluppo potrebbe essere comodo evitare l’invio vero della mail ma avere comunque un modo per verificare che il contenuto inviato coincida con quanto sperato. Per fare questo è possibile aggiungere un attributo all’elemento smtp perchè l’invio avvenga verso una cartella specificata:

image

Scegliendo come valore SpecifiedPickupDirectory e aggiungendo le informazioni sulla directory da utilizzare il sistema creerà un file .eml con il contenuto della mail:

<system.net>
  <mailSettings>
    <smtp from="test@foo.com" deliveryMethod="SpecifiedPickupDirectory">
      <specifiedPickupDirectory pickupDirectoryLocation="d:\mails" />
    </smtp>
  </mailSettings>
</system.net>

Le ragioni per utilizzare questa possibilità possono essere davvero tante:

  • Testare l’invio massimo di email
  • Verificare eventuali errori portando temporaneamente l’invio delle mail in un cartella per un sito in produzione
  • Durante lo sviluppo per evitare le attese tipiche dell’invio e della ricezione delle mail
  • Decidere di far processare l’invio delle mail nella cartella da un servizio separato

Naturalmente possiamo anche impostare il tutto da codice se volessimo ad esempio condizionare sotto if quale delivery method usare: basta impostare la proprietà DeliveryMethod dell’oggetto SmtpClient:

image

Print | posted on martedì 20 luglio 2010 21.35 |

Feedback

Gravatar

# re: Mandare una mail ad una cartella con Asp.Net

Decisamente interessante.
21/07/2010 0.43 | Mario Duzioni
Gravatar

# re: Mandare una mail ad una cartella con Asp.Net

However,ugg boots sale the always low-key people become media focus in early niche, not because he will ugg boots on sale occupy the people in this year's performance events mbt shoes discount and gl, nor he was hit "Confucius" people power "uncle wu",sacs à mains but in the last Christmas blog in a group of intimacy with CaoYing hunshazhao, but WangBan forsac à mains two people over ten years of marriage courtship hall has not been to answer. Recently, when he first interview Light plain boiled water about his emotional experiences. He clarified some misreading.NRL
23/07/2010 18.27 | fdsa

Post Comment

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

Powered by: