求C#中发送邮件(带附件)的代码

Devilzx 2007-04-16 11:37:43
如题,速度啊~,在线等
...全文
676 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
ssy888 2007-05-22
  • 打赏
  • 举报
回复
mark
wicket 2007-04-28
  • 打赏
  • 举报
回复
测试过了,已经通过了

<%@ 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>
 <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>
 
<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>

shushangdeyezi 2007-04-28
  • 打赏
  • 举报
回复
uping................
iloveaspx 2007-04-28
  • 打赏
  • 举报
回复
http://www.51aspx.com/CV/JH1GEZ9ACZA70
tianke3477 2007-04-28
  • 打赏
  • 举报
回复
大哥啊。。
client.Credentials = new NetworkCredential("用户名", "密码");
这个NetworkCredential是您自己定义的吧。。。
给个解释啊
-----------------------------------------------------------
using System.Net;
fxqyyzg 2007-04-16
  • 打赏
  • 举报
回复
MailMessage msgMail = new MailMessage();
msgMail.To = "test@test.com";
msgMail.From = "me@me.com";
msgMail.Subject = "Attachment Test";
msgMail.BodyFormat = MailFormat.Text;
msgMail.Body = "Check out the attachment!";
msgMail.Attachments.Add(new MailAttachment("c:\\test.xml"));
SmtpMail.Send(msgMail);
Response.Write("Email was queued to disk");
stoneeri 2007-04-16
  • 打赏
  • 举报
回复
看system.web.mail的说明,十分钟就可以写出来
Devilzx 2007-04-16
  • 打赏
  • 举报
回复
大哥啊。。
client.Credentials = new NetworkCredential("用户名", "密码");
这个NetworkCredential是您自己定义的吧。。。
给个解释啊
batiraul 2007-04-16
  • 打赏
  • 举报
回复
2005——

string server="smtp.163.com";
string file = "e:\xxx.rar";
MailMessage message = new System.Net.Mail.MailMessage("xxx@163.com", "yyy@163.com", "正文", "标题");
Attachment data = new System.Net.Mail.Attachment(file, System.Net.Mime.MediaTypeNames.Application.Octet);
System.Net.Mime.ContentDisposition disposition = data.ContentDisposition;
disposition.CreationDate = File.GetCreationTime(file);
message.Attachments.Add(data);
SmtpClient client = new SmtpClient(server);
client.Credentials = new NetworkCredential("用户名", "密码");
client.Send(message);
data.Dispose();
Devilzx 2007-04-16
  • 打赏
  • 举报
回复
这个是2003的吧,怎么感觉跟2005的有出入的地方很多啊。
给个2005的我,能直接用的。。
batiraul 2007-04-16
  • 打赏
  • 举报
回复
vs2003 引用 System.Web.Mail命名空间
vs2005 引用 System.Net.Mail命名空间,安全性得到了增强

...
try
{
//创建邮件消息
MailMessage objMailMessage = new MailMessage();
objMailMessage.From = System.Configuration.ConfigurationSettings.AppSettings["mailfrom"];//源邮件地址"xxx@163.com"
objMailMessage.To = System.Configuration.ConfigurationSettings.AppSettings["mailto"];//目的邮件地址"yyy@163.com"
objMailMessage.Subject = "邮件的标题";//发送邮件的标题
objMailMessage.Body = "邮件的内容";//发送邮件的内容

string user = System.Configuration.ConfigurationSettings.AppSettings["mailuser"];//源邮件地址用户名
string psw = (System.Configuration.ConfigurationSettings.AppSettings["mailpsw"]);//源邮件地址用户密码
//基本权限
objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
//用户名
objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", user) ;
//密码
objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", psw);
//如果没有上述三行代码,则出现如下错误提示:服务器拒绝了一个或多个收件人地址。服务器响应为: 554 : Client host rejected: Access denied
//SMTP地址
SmtpMail.SmtpServer = System.Configuration.ConfigurationSettings.AppSettings["mailsvr"];//"smtp.163.com"
//开始发送邮件
SmtpMail.Send(objMailMessage);
}
catch(Exception ex)
{
Log4Net.WriteLog("Exec_MailSend ERROR="+ex.ToString());
}
...

wthorse 2007-04-16
  • 打赏
  • 举报
回复
http://blog.csdn.net/wthorse/archive/2006/06/13/794705.aspx
took123 2007-04-16
  • 打赏
  • 举报
回复
我曾经用400分 求过相关的代码 都么找到

但有有个小例子 不完整你 你要吗?
Devilzx 2007-04-16
  • 打赏
  • 举报
回复
./...
这段代码就行了?

我试了 不行啊

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧