C#编写发送邮件组件
作者:hebedich 发布时间:2022-08-26 19:28:57
标签:C#,发送邮件
在MailSetting里的配置好邮件服务器,然后MailEntity里配置好要发送的邮件主体,最后使用MailServer里的方法Send发送邮件
MailEntity.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace AutoOutTicket.Mail
{
public class MailEntity
{
public string from;
public string to;
public string fromName;
public string toName;
public string cc;
public bool isHtml;
public string subject;
public string body;
public string attach;
}
}
MailServer.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
namespace AutoOutTicket.Mail
{
public class MailServer
{
MailEntity _entity = null;
MailSetting _settings = null;
public MailServer(MailEntity entity, MailSetting settings)
{
this._entity = entity;
this._settings = settings;
}
public bool Send()
{
try
{
MailMessage message = new MailMessage(_settings.smtpUser, _entity.to);
message.IsBodyHtml = _entity.isHtml;
message.Subject = _entity.subject;
message.Body = _entity.body;
if (!string.IsNullOrWhiteSpace(_entity.cc))
{
message.CC.Add(_entity.cc);
}
if (!string.IsNullOrWhiteSpace(_entity.attach))
{
Attachment atta=new Attachment(_entity.attach);
message.Attachments.Add(atta);
}
SmtpClient client = new SmtpClient(_settings.smtpHost, _settings.smtpPort);
client.Credentials = new NetworkCredential(_settings.smtpUser, _settings.smtpPass);
client.SendAsync(message, null);
return true;
}
catch (Exception)
{
}
return false;
}
}
}
MailSetting.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace AutoOutTicket.Mail
{
public class MailSetting
{
public string smtpHost = "";
public int smtpPort;
public string smtpUser = "";
public string smtpPass = "";
public MailSetting()
{
}
public MailSetting(string smtpServer, int smtpPort, string smtpUser, string smtpPass)
{
this.smtpHost = smtpServer;
this.smtpPort = smtpPort;
this.smtpUser = smtpUser;
this.smtpPass = smtpPass;
}
}
}
以上所述就是本文的全部内容了,希望大家能够喜欢。


猜你喜欢
- 本文适合有 Java 基础的人群作者:DJL-LankingHelloGitHub 推出的《讲解开源项目》系列。有幸邀请到了亚马逊 + Ap
- /** * 三角数字: * 比达哥斯拉领导下的古希腊数学家发现了一个有趣的数字序列1, 3, 6, 10, 15, 21,... *
- 本文主要介绍了C# 数组删除元素的实现示例,具体如下:using System;using System.Collections.Gener
- BigDecimal 和 0 比较大小调用BigDecimal中的compareTo方法, 如:int i = bigDecimal.com
- 详细描述maven中央仓库发布jar包的中间过程, 以及遇到的一些问题汇总, 尽量用文字描述清楚, 耐心看下去, 就一定会发布成功----S
- 现在Android上架各大平台都要求App首页添加一个弹框,显示用户协议以及一些隐私政策,不然上架各大平台,现在就来简单的实现一下这个对话框
- 介绍Java中的享元模式(Flyweight Pattern)是一种结构型设计模式,旨在通过共享尽可能多的对象来减少内存占用和提高性能.Ja
- 项目地址: GITHUB (本地下载)java mybatis 多表查询简介实现简单的实体类操作多表, 首先你的项目是
- 数据层测试事务回滚pom.xml导入对应的一些坐标,mysql,Mp,等<dependency> &
- 本文实例讲述了C#通过属性名字符串获取、设置对象属性值操作.分享给大家供大家参考,具体如下:#通过反射获取对象属性值并设置属性值0、定义一个
- 一. ANR场景无论是四大组件或者进程等只要发生ANR,最终都会调用AMS.appNotResponding()方法,下面从这个方法说起。以
- Java的常量池通常分为两种:静态常量池和运行时常量池静态常量池:class文件中的常量池,class文件中的常量池包括了字符串(数字)字面
- 前言在我们平时使用图形化界面的时候,会发现来建立一个文件夹或者一个文档的时候很简单,只需要在桌面单击鼠标右键就可以了。但是,在我们写项目的时
- springboot动态调用实现类定义规则的多种类型/** * 数据规则处理类型枚举 */public enum RuleType { &n
- 目录1. 结论先出JSR 380Valid VS Validated 不同点?Validator2. @Valid和@Validated 注
- 废话不多说了,直接给大家贴关键代码了。具体代码如下所示:using System;using System.Collections.Gene
- 1、什么是 生命周期?Maven 强大的原因是有一个十分完善的生命周期,生命周期可以理解为项目构建步骤的集合,它定义了各个构建环节的执行顺序
- 循环轮播的方法有两种,一种是使用定时器另外一种是使用手指拨动,相比较而言,定时器实现循环播放比较容易,只要在定时器的消息里加上简单几段代码即
- 关于jmeter中的正则表达式及json提取器可以提取响应值,大家都有所了解,但是往往我们在实际运用中,可能需要上个接口的多个响应值,难道我
- 这篇文章主要介绍了JAVA利用递归删除文件代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可