您现在的位置是:网站首页> 编程资料编程资料
asp.net发送邮件示例分享_实用技巧_
2023-05-24
302人已围观
简介 asp.net发送邮件示例分享_实用技巧_
mailhelper -------mail帮助类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mail;
///
///mailhelper 的摘要说明
///
public class mailhelper
{
public mailhelper()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
///
/// 邮件发送操作
///
/// 收件人地址
/// 发件人地址
/// 发件人密码
/// 抄送人地址
/// 密送人地址
/// 发送主题
/// 附件信息
/// 邮件内容
public string SendeEmal(string Addressee, string From, string sendpassword, string Copy, string secret, string Subject, string Attachment, string Body)
{
MailMessage objMailMessage;
MailAttachment objMailAttachment;
// 创建邮件消息
objMailMessage = new MailMessage();
//发件人EMAIL
objMailMessage.From = From;//源邮件地址
//收件人EMAIL
objMailMessage.To = Addressee; //目的邮件地址
//邮件抄送
objMailMessage.Cc = Copy;
//邮件misong
objMailMessage.Bcc = secret;
//邮件主题
objMailMessage.Subject = Subject; //发送邮件的标题
//邮件内容
objMailMessage.Body = Body;//发送邮件的内容
// 创建一个附件对象
if (Attachment != "")
{
objMailAttachment = new MailAttachment(Attachment);//发送邮件的附件 c:\\test.txt
objMailMessage.Attachments.Add(objMailAttachment);//将附件附加到邮件消息对象中
}
//接着利用SMTP来发送邮件,需要使用Microsoft .NET Framework SDK v1.1和它以上的版本
//基本权限
objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
//用户名
string name = From.Substring(0, From.IndexOf('@'));
objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", name);
//密码
objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", sendpassword);
//如果没有上述三行代码,则出现如下错误提示:服务器拒绝了一个或多个收件人地址。服务器响应为: 554 : Client host rejected: Access denied
//SMTP地址
string smtp = "smtp." + From.Substring(From.IndexOf('@') + 1);
SmtpMail.SmtpServer = "smtp." + From.Substring(From.IndexOf('@') + 1);
//开始发送邮件
try
{
SmtpMail.Send(objMailMessage);
return "邮件发送成功!";
}
catch (System.Net.Mail.SmtpException ex)
{
return ex.Message;
}
//核心代码结束
}
}
然后下来是自己做的一个demo--
前台
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="mail.aspx.cs" Inherits="information_mail"
ValidateRequest="false" %>
后台:
protected void Button1_Click(object sender, EventArgs e)
{ //实例邮件帮助类
mailhelper mails = new mailhelper();
string filePath = HiddenField1.Value;
string a = mails.SendeEmal(TextBox1.Text, "邮件账号", "邮件密码", TextBox2.Text, TextBox4.Text, TextBox5.Text, filePath, TextBox3.Text);
Label1.Text = a;
}
相关内容
- wireshark抓取本地回环数据包和取出数据的方法_实用技巧_
- asp.net datalist绑定数据后可以上移下移实现示例_实用技巧_
- asp.net传多个值到其它页面的具体实现_实用技巧_
- ASP.net判断上传文件类型的三种有效方法_实用技巧_
- 如何将数据绑到gridview然后导成excel_实用技巧_
- Asp.net禁用页面缓存的方法总结_实用技巧_
- ASP.Net中表单POST到其他页面的方法分享_实用技巧_
- asp.net页面与页面之间传参数值方法(post传值和get传值)_实用技巧_
- asp.net字符串分割函数使用方法分享_实用技巧_
- php基础练习--简单验证码实现_实用技巧_
