在asp.net中发送电子邮件的代码是什么 [英] What is the code for sending email in asp.net
本文介绍了在asp.net中发送电子邮件的代码是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在使用asp.net发送电子邮件时遇到问题,请帮助我.........
I have problem with sending email with asp.net please help me.........
推荐答案
protected void btnSendmail_Click(object sender, EventArgs e)
{
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
MailAddress fromAddress = new MailAddress(txtEmail.Text, txtName.Text);
smtpClient.Host = "smtp.gmail.com";
message.From = fromAddress;
message.To.Add("admin1@yoursite.com");
message.Subject = "Feedback";
message.Body = txtMessage.Text;
// Send SMTP mail
smtpClient.Send(message);
lblStatus.Text = "Email successfully sent.";
}
catch (Exception ex)
{
lblStatus.Text = "Send Email Failed." + ex.Message;
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
using System.Net.Mime;
namespace Gmail
{
public partial class Form1 : Form
{
//String path;
MailMessage mail = new MailMessage();
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
SmtpClient SmtpServer = new SmtpClient();
SmtpServer.Credentials = new System.Net.NetworkCredential("your e-mail id", "password");
SmtpServer.Port = 587;
SmtpServer.Host = "smtp.gmail.com";
SmtpServer.EnableSsl = true;
mail = new MailMessage();
String[] addr = TextBox1.Text.Split('','');
try
{
mail.From = new MailAddress("your email id", "Developers", System.Text.Encoding.UTF8);
Byte i;
for( i = 0;i< addr.Length; i++)
mail.To.Add(addr[i]);
mail.Subject = TextBox3.Text;
mail.Body = TextBox4.Text;
if(ListBox1.Items.Count != 0)
{
for(i = 0;i<listbox1.items.count;i++)>
mail.Attachments.Add(new Attachment(ListBox1.Items[i].ToString()));
}
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
mail.ReplyTo = new MailAddress(TextBox1.Text);
SmtpServer.Send(mail);
MessageBox.Show("Mail Send");
}
catch (Exception ex){
MessageBox.Show(ex.ToString());
}
}
}
}
您可以尝试以下方法:
You can try this method:
public Boolean SendingMail(string From, string To, string Subject,string Body)
{
try
{
MailMessage m = new MailMessage("mailidofSender", To);
m.Subject = Subject;
m.Body = Body;
m.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "mail.google.com";
NetworkCredential authinfo = new NetworkCredential("mailidfrom", "YourPassword");
smtp.UseDefaultCredentials = false;
smtp.Credentials = authinfo;
smtp.Send(m);
return true;
}
catch (Exception ex)
{
return false;
}
}
这篇关于在asp.net中发送电子邮件的代码是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文