需要c#代码才能在桌面使用gmail向许多人发送邮件 [英] need c# code for sending mails to many using gmail in Desktop
本文介绍了需要c#代码才能在桌面使用gmail向许多人发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
需要源代码将邮件发送到许多地方,检索发件人地址与文本框中的逗号分隔
[edit]它显示需要smtp身份验证[ / edit]
Need source code to send mails to many where am retrieving the sender address seperated by a comma from a text box
[edit] It shows smtp authentication needed [/edit]
public partial class Mail : Form
{
public string s;
public SmtpClient client = new SmtpClient();
public MailMessage msg = new MailMessage();
String[] email = new String[30];
int flag = 0;
public System.Net.NetworkCredential smtpcrede = new System.Net.NetworkCredential("username", "Password");
//Sending mail
private void SendEmail(string sendTo, string sendFrom, string subject, string body)
{
try
{
client.Host = "smtp.gmail.com";
client.Port = 587;
client.UseDefaultCredentials = false;
client.Credentials = smtpcrede;
client.EnableSsl = true;
MailAddress to = new MailAddress(sendTo);
MailAddress from = new MailAddress(sendFrom);
msg.IsBodyHtml = true;
msg.Subject = subject;
msg.Body = body;
msg.From = from;
string[] addrs = sendTo.Split(',');
foreach (string addr in addrs)
{
MailAddress to = new MailAddress(addr);
msg.To.Add(to);
}
client.Send(msg);
}
//send email
private void btn_Send_Click(object sender, EventArgs e)
{
String mailmsg = "text1<br/>"+rich_Msg.Text+"<br/>text2";
for (int i = 0; i < email.Length; i++)
{
SendEmail(email[i], "from@mail.com", txt_Subj.Text,mailmsg);
// Console.WriteLine(email[i], "from@mail.com", txt_Subj.Text, rich_Msg.Text);
}
推荐答案
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.Net.Mail;
public partial class _Default : System.Web.UI.Page
{
string type;
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Enabled = false;
TextBox2.Enabled = false;
TextBox3.Enabled = false;
TextBox4.Enabled = false;
TextBox5.Enabled = false;
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
if (DropDownList1.SelectedValue == "gmail")
{
type = "smtp.gmail.com";
}
else
{
type = "smtp.live.com";
}
MailMessage msg = new MailMessage();
msg.From = new MailAddress(TextBox1.Text);
msg.To.Add(TextBox2.Text);
msg.Subject = TextBox4.Text;
msg.Body = TextBox3.Text;
SmtpClient sc = new SmtpClient(type);
sc.Port = 25;
sc.Credentials = new NetworkCredential(TextBox1.Text, TextBox5.Text);
sc.EnableSsl = true;
sc.Send(msg);
Response.Write("Mail sent");
}
catch (Exception e1)
{
Response.Write(e1.Message);
}
}
protected void Button2_Click(object sender, EventArgs e)
{
TextBox1.Enabled = true;
TextBox2.Enabled = true;
TextBox3.Enabled = true;
TextBox4.Enabled = true;
TextBox5.Enabled = true;
}
}
如果你想发送给多个人..只需在MailMessage部分添加一个循环,直到Response.Write(Mail发送);
如果能解决问题,请接受答案
If u want to send to multiple people..just add a loop in the MailMessage section upto Response.Write("Mail sent");
Accept as answer if it solves your problem
这篇关于需要c#代码才能在桌面使用gmail向许多人发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文