首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

发邮件有关问题,兄弟们进

2012-04-02 
发邮件问题,兄弟们进我用asp.net发邮件(网上很多相关代码),但全都不能用gmail发送或者用gmail接收。不知道

发邮件问题,兄弟们进
我用asp.net发邮件(网上很多相关代码),但全都不能用gmail发送或者用gmail接收。不知道哪位有好的办法。
要用using   System.Net.Mail;


[解决办法]
接收邮件 (POP3) 服务器 - 需要 SSL: pop.gmail.com
使用 SSL:是
端口:995
发送邮件 (SMTP) 服务器 - 需要 TLS: smtp.gmail.com(使用验证)
使用验证:是
使用 STARTTLS:是(某些客户端称其为 SSL)
端口:465 或 587
帐户名: 您的 Gmail 用户名(包括 @gmail.com)
电子邮件地址: 您的完整 Gmail 电子邮件地址(用户名@gmail.com)
密码: 您的 Gmail 密码

[解决办法]
MailMessage msg = new MailMessage();
msg.From = lblFrom.Text;
msg.Bcc = txtEmailName.Text;
msg.Subject = txtEmailSubject.Text.Trim();
msg.Body = txtContent.Text.Trim().Replace( " <br> ", "\r\n ");
msg.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate ", "1 " );
msg.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/sendusername ", lblMailServerUserName.Text);
msg.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/sendpassword ", lblMailServerPassWord.Text);
msg.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/smtpserverport ", lblport.Text);
if(lblssl.Text.Equals( "true "))
msg.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/smtpusessl ", "1 ");
SmtpMail.SmtpServer = lblSmtp.Text;
foreach( ListItem temp in AffixList.Items )
{
if( temp != null )
{
string attachFile=temp.Value;
MailAttachment mailAttach=new MailAttachment(attachFile);
msg.Attachments.Add(mailAttach);
}
}
try
{
SmtpMail.Send(msg);
utility.Method.ExampleJavascript(Page, "發送成功! ", "EmailSend.aspx ");
}
catch
{
utility.Method.ExampleJavascript(Page, "發送失敗,請檢查您填寫的信箱地址! ",true);
}
finally
{
delFile();
msg = null;
}
[解决办法]
我用这个发 没问题

//Builed The MSG
MailMessage msg = new System.Net.Mail.MailMessage();
msg.To.Add( "toaddress@gmail.com ");
msg.From = new MailAddress( "fromaddress@gmail.com ", "from ", System.Text.Encoding.UTF8);
msg.Subject = "subject ";
msg.SubjectEncoding = System.Text.Encoding.UTF8;
msg.Body = "body ";
msg.BodyEncoding = System.Text.Encoding.UTF8;
msg.IsBodyHtml = true;
msg.Priority = MailPriority.High;

//Add the Creddentials
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential( "username ", "password ");
client.Port = 587;//or use 587
client.Host = "smtp.gmail.com ";
client.EnableSsl = true;
//client.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);
//object userState = msg;
try
{
//you can also call client.Send(msg)
//client.SendAsync(msg, userState);
client.Send(msg);
}
catch (System.Net.Mail.SmtpException ex)
{
Response.Write(ex.ToString());
}

[解决办法]
http://www.codeproject.com/useritems/SendMailUsingGmailAccount.asp
------解决方案--------------------


楼上的几未也都可以 我也是用上面的方法!!
[解决办法]
测试过了,已经通过了

<%@ Page Language= "C# " %>

<%@ Import Namespace= "System.Net.Mail " %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<script runat= "server ">



protected void Button1_Click(object sender, EventArgs e)
{
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();

msg.From = new MailAddress( "yuzhen20@126.com ");
msg.To.Add(new MailAddress(TextBox1.Text));

msg.Subject = TextBox2.Text;
msg.Body = TextBox3.Text;
string attachFile;
attachFile=FileUpload1.FileName;

System.Net.Mail.Attachment mailadd = new Attachment(FileUpload1.PostedFile.InputStream, attachFile);


msg.Attachments.Add(mailadd);

SmtpClient smt = new SmtpClient();
smt.Send(msg);




}
</script>

<html xmlns= "http://www.w3.org/1999/xhtml " >
<head runat= "server ">
<title> 无标题页 </title>
</head>
<body>
<form id= "form1 " runat= "server ">
<div>
&nbsp; <asp:Label ID= "Label1 " runat= "server " Text= "收件人地址: "> </asp:Label>
<asp:TextBox ID= "TextBox1 " runat= "server " Width= "216px "> </asp:TextBox> <br />
<br />
<asp:Label ID= "Label2 " runat= "server " Text= "主题: "> </asp:Label>
<asp:TextBox ID= "TextBox2 " runat= "server " Width= "219px "> </asp:TextBox> <br />
<br />
<asp:Label ID= "Label4 " runat= "server " Text= "附件: "> </asp:Label>
&nbsp;
<asp:FileUpload ID= "FileUpload1 " runat= "server " /> <br />
<br />
<asp:Label ID= "Label3 " runat= "server " Text= "内容: "> </asp:Label> <br />
<asp:TextBox ID= "TextBox3 " runat= "server " Height= "176px " Width= "348px "> </asp:TextBox> <br />
<br />
<br />
<asp:Button ID= "Button1 " runat= "server " Text= "发送 " OnClick= "Button1_Click " />
<asp:Button ID= "Button2 " runat= "server " Text= "取消 " /> </div>
</form>
</body>
</html>

然后在web.config里面配置下:
<?xml version= "1.0 "?>
<configuration>
<system.net>
//配置smtp
<mailSettings>
<smtp deliveryMethod= "Network " from= "yuzhen20@126.com ">
<network defaultCredentials= "false " host= "smtp.126.com " port= "25 " userName= "yuzhen20 " password= "tramper11 "/>

</smtp>
</mailSettings>
</system.net>

<connectionStrings/>
<system.web>
<globalization requestEncoding= "gb2312 " responseEncoding= "gb2312 "/>


<compilation debug= "true "/>
<authentication mode= "Windows "/>

<customErrors mode= "RemoteOnly " defaultRedirect= "GenericErrorPage.htm ">
<error statusCode= "403 " redirect= "NoAccess.htm " />
<error statusCode= "404 " redirect= "FileNotFound.htm " />
</customErrors>
-->
</system.web>
</configuration>

热点排行