Este non é exactamente un novo tema, pero cando eu precisaba facer iso, I found a lot of “why won’t this work for me” and not too many direct answers. I hope someone finds this useful.
O seguinte fragmento de código pode enviar un correo-e usando a miña conta de Gmail para facelo, incluíndo anexos:
utilización System.Net.Mail; utilización System.Net; NetworkCredential loginInfo = novo NetworkCredential("[O meu ID Gmail]", "[O meu contrasinal de Gmail]"); MailMessage msg = novo MailMessage(); msg.From = novo MailAddress("[M Gmail Id]@ Gmail.com"); msg.To.Add(novo MailAddress("paul.galvin@arcovis.com")); msg.Subject = "Test infopath dev subject"; msg.Body = "<html><corpo><forte>Unha mensaxe forte.</forte></corpo></html>"; msg.IsBodyHtml = certo; foreach (corda aguçar en NIPFD.GetAttachmentNamesAndLocations()) { msg.Attachments.Add(novo Accesorios(aguçar)); } // Engadido anexos. SmtpClient client = novo SmtpClient("smtp.gmail.com"); client.EnableSsl = certo; client.UseDefaultCredentials = teito; client.Credentials = loginInfo; client.Port = 587; client.EnableSsl = certo; client.Send(mensaxe); |
Algúns bits clave que retardou-me para abaixo e outras observacións / notas:
- A primeira liña que crea o obxecto loginInfo que usar o seu ID de Gmail desposuído de "@ Gmail.com". Así, o meu enderezo de correo-e de Gmail e "Sharepoint@gmail.com"E a miña contrasinal" xyzzy ", entón a liña quedaría así:
NetworkCredential loginInfo = novo NetworkCredential("sharepoint", "Xyzzy");
- A miña conta de Gmail está configurado para usar SSL e que non era un problema.
- There is some conflicting information out there on what port to use. I used port 587 e el funcionou ben para min.
- No meu caso, I also needed to send attachments. That NIPFD object has a method that knows where my attachments are. It’s returning a fully path (e.g. "C:\temp\attachment1.jpg”. In my test, Eu tiña dous anexos e ambos funcionaron ben.
Eu usei o Visual Studio 2008 para escribir este código.
</final>
Siga-me no Twitter http://www.twitter.com/pagalvin
Clean and simple…
Grazas por compartir.
The PowerShell Team blog recently had a similar post that I found Invaluable as a server admin. Visual Studio is great but many of my peers are not developers and Incluír tamén a license purchased. PowerShell Feels more like the command line and is more accessible to them. Just a Suggestion for any Admins out there.
O mellor,
Jeff (www.spjeff.com / @ Spjeff)
Sending Automated emails with Send-MailMessage
http://blogs.msdn.com/powershell/archive/2009/10/30/sending-automated-emails-with-send-mailmessage-convertto-html-and-the-powershellpack-s-taskscheduler-module.aspx