发送电子邮件的代码 [英] code for sending email
本文介绍了发送电子邮件的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
解决方案
这里有一个发送电子邮件的通用例程: ^ ]
尝试此代码
Dim mes As 字符串 = 字符串.空 Dim sTo As 字符串 = " Dim sFrom As 字符串 = " Dim sSubject As 字符串 = " Dim sMailServer As 字符串 = " Dim MyMail As System.Web.Mail.MailMessage = 新 System.Web.Mail.MailMessage MyMail.From = sFrom MyMail.收件人 = sTo MyMail.Subject = sSubject MyMail.Body = " MyMail.BodyEncoding = Encoding.UTF8 MyMail.BodyFormat = MailFormat.Html SmtpMail.SmtpServer = sMailServer 尝试 SmtpMail.Send(MyMail) PRes.InnerHtml = " 捕获,例如 As 异常 PRes.InnerHtml = " 结束 尝试
谢谢.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Mail; namespace MyMail { class SendMail { SmtpClient objSmtpClient = new SmtpClient(); MailMessage objMailMessage = new MailMessage(); MailAddress objMailAddress; public SendMail() { // objSmtpClient.Host = ""; // objSmtpClient.Port = ; objSmtpClient.Host = "smtp.gmail.com"; objSmtpClient.Port = 465; } public bool Sendmail(string mail_From,string mail_To,string mail_Password,string mail_Subject,string mail_Message) { objMailAddress = new MailAddress(mail_From); System.Net.NetworkCredential objNetworkCredential = new System.Net.NetworkCredential(mail_From, mail_Password); objSmtpClient.UseDefaultCredentials = false; objSmtpClient.Credentials = objNetworkCredential; objMailMessage.From = objMailAddress; objMailMessage.Subject = mail_Subject; objMailMessage.Body = mail_Message; objMailMessage.IsBodyHtml = false; objMailMessage.To.Add(mail_To); try { objSmtpClient.Send(objMailMessage); return true; } catch (Exception e) { return false; } } } }
对上述代码进行必要的更改.
hi I am sangeetha. I have been creating a website.so i need a text provision for user to enter their complaints and feedback and that should sent to administrators email id.So i need code for sending that complaint to an email id and please specify the namespace and class need for that.Please help me
解决方案
There is a generic routine for sending email here: Sending an Email in C# with or without attachments: generic routine.[^]
Try This code
Dim mes As String = String.Empty Dim sTo As String = "[To Address]" Dim sFrom As String = "[From Address]" Dim sSubject As String = "[Subject]" Dim sMailServer As String = "127.0.0.1" Dim MyMail As System.Web.Mail.MailMessage = New System.Web.Mail.MailMessage MyMail.From = sFrom MyMail.To = sTo MyMail.Subject = sSubject MyMail.Body = "[Your Message]" MyMail.BodyEncoding = Encoding.UTF8 MyMail.BodyFormat = MailFormat.Html SmtpMail.SmtpServer = sMailServer Try SmtpMail.Send(MyMail) PRes.InnerHtml = "Your Mail is Successfully Sended" Catch ex As Exception PRes.InnerHtml = "Your Mail is not Sended. Because Of this Error : " & ex.Message End Try
Thanks.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Mail; namespace MyMail { class SendMail { SmtpClient objSmtpClient = new SmtpClient(); MailMessage objMailMessage = new MailMessage(); MailAddress objMailAddress; public SendMail() { // objSmtpClient.Host = ""; // objSmtpClient.Port = ; objSmtpClient.Host = "smtp.gmail.com"; objSmtpClient.Port = 465; } public bool Sendmail(string mail_From,string mail_To,string mail_Password,string mail_Subject,string mail_Message) { objMailAddress = new MailAddress(mail_From); System.Net.NetworkCredential objNetworkCredential = new System.Net.NetworkCredential(mail_From, mail_Password); objSmtpClient.UseDefaultCredentials = false; objSmtpClient.Credentials = objNetworkCredential; objMailMessage.From = objMailAddress; objMailMessage.Subject = mail_Subject; objMailMessage.Body = mail_Message; objMailMessage.IsBodyHtml = false; objMailMessage.To.Add(mail_To); try { objSmtpClient.Send(objMailMessage); return true; } catch (Exception e) { return false; } } } }
make necessary changes for the above code.
这篇关于发送电子邮件的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文