Vinnige en maklike: Stuur e-pos gebruik Gmail SMTP-bediener in. NET C #

Dit is nie juis 'n nuwe onderwerp, maar toe ek dit nodig gehad het om te doen, I found a lot of “why won’t this work for me” and not too many direct answers. I hope someone finds this useful.

Die volgende stukkie van die kode sal stuur 'n e-pos met my eie Gmail-rekening om dit te doen, insluitend aanhegsels:

die gebruik van System.Net.Mail;
die gebruik van System.Net;

NetworkCredential loginInfo = nuwe NetworkCredential("[My Gmail ID]", "[My Gmail Vergeet]");
Mailbericht msg = nuwe Mailbericht();
msg.From = nuwe Pos adres("[M Gmail-id]@ Gmail.com");
msg.To.Add(nuwe Pos adres("paul.galvin@arcovis.com"));
msg.Subject = "Test infopath dev subject";
msg.Body = "<html><liggaam><sterk>'N sterk boodskap.</sterk></liggaam></html>";
msg.IsBodyHtml = waar;

foreach (string AFile in NIPFD.GetAttachmentNamesAndLocations())
{
    msg.Attachments.Add(nuwe Beslaglegging(AFile));
} // Voeg aanhegsels.

SmtpClient kliënt = nuwe SmtpClient("smtp.gmail.com");
client.EnableSsl = waar;
client.UseDefaultCredentials = valse;
client.Credentials = loginInfo;
client.Port = 587;
client.EnableSsl = waar;
client.Send(msg);

A few key bits that slowed me down and other observations / notes:

  • The first line that creates the loginInfo object needs to use the gmail ID stripped of “@gmail.com". So, if my gmail email address is “sharepoint@gmail.com” and my password is “xyzzy” then the line would look like:

NetworkCredential loginInfo = nuwe NetworkCredential("sharepoint", "Xyzzy");

  • My gmail account is set up to use SSL and that wasn’t a problem.
  • There is some conflicting information out there on what port to use. I used port 587 and it worked fine for me.
  • In my geval, I also needed to send attachments. That NIPFD object has a method that knows where my attachments are. It’s returning a fully path (bijv. “c:\temp\attachment1.jpg”. In my test, Ek het twee aanhegsels en hulle albei werk goed.

Ek gebruik van Visual Studio 2008 hierdie kode te skryf.

</einde>

Skryf in op my blog.

Volg my op Twitter http://www.twitter.com/pagalvin

Technorati Tags: ,,,

2 gedagtes oor "Vinnige en maklike: Stuur e-pos gebruik Gmail SMTP-bediener in. NET C #

  1. Jeff

    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 don’t have a license purchased. PowerShell feels more like the command line and is more accessible to them. Just a suggestion for any admins out there.

    Best,
    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

    Antwoord

Laat 'n antwoord

Jou e-posadres sal nie gepubliseer word nie. Verpligte velde gemerk *