如何不使用密码发送邮件

时间:2016-01-25 09:29:15

标签: c# asp.net-mvc asp.net-mvc-4

我想发送邮件而不使用mailid的密码,目前我使用此代码发送邮件.....我硬编码邮件ID和密码

我想传递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);

请帮帮我

2 个答案:

答案 0 :(得分:0)

忽略设置凭据并使用:

smtp.UseDefaultCredentials = true;

这样的事情: https://msdn.microsoft.com/en-us/library/swas0fwc(v=vs.110).aspx#code-snippet-2

答案 1 :(得分:0)

这是您可以使用的示例代码。如果您有任何疑问,请询问!

// 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;