Tapaidh agus éasca: Seol Ríomhphost Ag baint úsáide as Gmail SMTP Server in. GLAN C #

Ní hé seo go díreach le topaic nua, ach nuair is gá dom a dhéanamh air, I found a lot of “why won’t this work for me” and not too many direct answers. I hope someone finds this useful.

Beidh an beagán a leanas de chód sheoladh r-phost ag baint úsáide as mo chuntas gmail féin é a dhéanamh, lena n-áirítear aidhlicí:

ag baint úsáide as System.Net.Mail;
ag baint úsáide as System.Net;

NetworkCredential loginInfo = nua NetworkCredential("[Mo haitheantas ar Gmail]", "[Mo Pasfhocal Gmail]");
MailMessage msg = nua MailMessage();
msg.From = nua MailAddress("[M Gmail Id]@ Gmail.com");
msg.To.Add(nua MailAddress("paul.galvin@arcovis.com"));
msg.Subject = "Test infopath dev subject";
msg.Body = "<html><comhlacht><láidir>A teachtaireacht láidir.</láidir></comhlacht></html>";
msg.IsBodyHtml = fíor;

foreach (teaghrán aFile i NIPFD.GetAttachmentNamesAndLocations())
{
    msg.Attachments.Add(nua Attachment(aFile));
} // Iatáin a chuirfear.

SmtpClient cliant = nua SmtpClient("smtp.gmail.com");
client.EnableSsl = fíor;
client.UseDefaultCredentials = bréagach;
client.Credentials = loginInfo;
client.Port = 587;
client.EnableSsl = fíor;
client.Send(msg);

A giotán roinnt eochair que mhoilligh mé síos agus tuairimí eile / nótaí:

  • Cruthaíonn an chéad líne de dhíth ar an réad loginInfo que a bhaint as an id gmail stripped "@ Gmail.com". Mar sin,, má tá mo sheoladh ríomhphoist gmail "Sharepoint@gmail.com"Agus is é mo phasfhocal" xyzzy "ansin bheadh ​​an líne cuma mhaith:

NetworkCredential loginInfo = nua NetworkCredential("sharepoint", "xyzzy");

  • Is é mo chuntas gmail ar bun chun úsáid SSL agus ní raibh que fadhb.
  • There is some conflicting information out there on what port to use. I used port 587 agus d'oibrigh sé fíneáil do dom.
  • I mo chás, 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, Bhí mé dhá iatáin agus iad araon, d'oibrigh fíneáil.

Bhain mé úsáid as stiúideo amhairc 2008 a scríobh cód seo.

</deireadh>

Liostáil le mo bhlag.

Lean mé ar Twitter ag http://www.twitter.com/pagalvin

Clibeanna Technorati: ,,,

2 smaointe ar "Tapaidh agus éasca: Seol Ríomhphost Ag baint úsáide as Gmail SMTP Server in. GLAN 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.

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

    Freagra

Leave a Reply

Ní thabharfar do sheoladh r-phoist a fhoilsiú. Réimsí riachtanacha atá marcáilte *