如何在不使用密码的情况下发送邮件
本文关键字:情况下 密码 | 更新日期: 2023-09-27 18:31:48
我想在不使用mailid密码的情况下发送邮件,目前我使用此代码发送邮件.....我硬编码邮件 ID 和 PWD
我想将 frommail 和 tomail 作为参数传递,并且我不想使用密码来发送邮件
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("demo@outlook.com", "demo1234");
SmtpClient smtp = new SmtpClient("smtp-mail.outlook.com", 587);
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Credentials = cred;
smtp.Send(message);
请帮助我
忽略设置凭据并使用:
smtp.UseDefaultCredentials = true;
像这样:https://msdn.microsoft.com/en-us/library/swas0fwc(v=vs.110).aspx#code-snippet-2
这是一个您可以使用的示例代码。如果您有任何问题,请询问!
// Create the Outlook application by using inline initialization.
Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.MailItem oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
Microsoft.Office.Interop.Outlook.Inspector oInspector = oMsg.GetInspector;
Microsoft.Office.Interop.Outlook.Recipient oTORecipt = oMsg.Recipients.Add(email);
oTORecipt.Type = (int)Microsoft.Office.Interop.Outlook.OlMailRecipientType.olTo;
oTORecipt.Resolve();
oMsg.Subject = "Happy Birthday";
oMsg.HTMLBody = body; //copy the HTML msg body taken from the HTML file. Body is a string
string date = DateTime.Today.ToString("MM-dd-yyyy");
oMsg.Importance = Microsoft.Office.Interop.Outlook.OlImportance.olImportanceHigh;
oMsg.Save();
oMsg.Display();
//Explicitly release objects.
oMsg = null;
oApp = null; //This has to be done at the end
请记住添加头文件using Microsoft.Office.Interop.Outlook;