MailMessage message = new MailMessage(sender, recipient, subject, msg); SmtpClient mySmtpClient = new SmtpClient(mailServer); mySmtpClient.UseDefaultCredentials = true; mySmtpClient.Send(message);
SmtpClient smtp = new SmtpClient("127.0.0.1"); smtp.UseDefaultCredentials = true; smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis; smtp.Port = 25; MailMessage msg = new MailMessage("somebody@emailhost.com", "somebody@emailhost.com", "My Email Subject", "This is a test of my SMTP service."); try { smtp.Send(msg); MessageBox.Show("Message sent!"); }
Imports System.Net Imports System.Net.Mail ... Public Sub SendEmail(ByVal recipient As String, ByVal sender As String, ByVal subject As _ String, ByVal messageBody As String, ByVal smtpServer As String) Dim smtpClient As New SmtpClient smtpClient.Host = smtpServer smtpClient.Port = 25 smtpClient.Credentials = CredentialCache.DefaultNetworkCredentials Dim mailMessage As New MailMessage(sender, recipient, subject, messageBody) mailMessage.IsBodyHtml = True smtpClient.Send(mailMessage) End Sub
web.config
SmtpClient c = new SmtpClient()
<system.net> <mailSettings> <smtp deliveryMethod="Network" > <network host="10.0.0.2" port="25" defaultCredentials="true" /> </smtp> </mailSettings> </system.net>
userName
password
<system.net> <mailSettings> <smtp deliveryMethod="Network" from="sender@email.com" > <network host="email-smtp.us-east-1.amazonaws.com" port="587" userName="AKIA*****" password="*****" enableSsl="true" /> </smtp> </mailSettings> </system.net>