雨心

c#调用阿里云推送发送邮件
效果要求一个域名一个阿里云账号引入阿里云SDK进入阿里云找到邮件推送服务新建一个发信域名和一个发信地址关键代码pr...
扫描右侧二维码阅读全文
22
2019/02

c#调用阿里云推送发送邮件

效果

9B32F1D6-30E1-4FCC-95BB-FC0CAAEEF8.png
9B2B8C2D-2B0E-4E8B-B897-E49A8D26B2BA.png

要求

  1. 一个域名
  2. 一个阿里云账号
  3. 引入阿里云SDK

进入阿里云找到邮件推送服务

0B9C12A9-07FD-46FC-856D-C151DC9C63E6.png
新建一个发信域名和一个发信地址

关键代码

private void btnEmailReset_Click(object sender, EventArgs e)
        {
            Random rd = new Random();
            newpassword = (rd.Next(10000000, 100000000)).ToString();//随机生成一个8位数字密码
            DataSet ds = DBOperate.readDB("select * from UserInfo where UserEmail='" + txtEmailReset.Text.Trim() + "'");
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (DBOperate.writeDB("update UserInfo set UserPassword='" + newpassword + "' where UserEmail='" + txtEmailReset.Text.Trim() + "'") > 0)
                {
                    IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", "阿里云", "密钥");
                    IAcsClient client = new DefaultAcsClient(profile);
                    SingleSendMailRequest request = new SingleSendMailRequest();
                    try
                    {
                        String pass = "<table style=\";width:99.8%;height:99.8%\";><tbody><tr><td style=\";background:#fafafa url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAYAAADED76LAAAAy0lEQVQY0x2PsQtAYBDFP1keKZfBKIqNycCERUkMKLuSgZnRarIpJX8s3zfcDe9+794du+8bRVHQOI4wDAOmaULTNDDGYFkWMVVVQUTQdZ3iOMZxHCjLElVV0TRNYHVdC7ptW6RpSn3f4wdJkiTs+w6WJAl4DcOAbdugKAq974umaRAEARgXn+cRW3zfFxuiKCJZloXGHMeBbdv4Beq6Duu6Issy7iYB8Jbnucg8zxPLsggnj/zvIxaGIXmeB9d1wSE+nOeZf4HruvABUtou5ypjMF4AAAAASUVORK5CYII=)\";><div style=\";border-radius:10px;font-size:13px;color:#555;width:666px;font-family:'Century Gothic','Trebuchet MS','Hiragino Sans GB','微软雅黑','Microsoft Yahei',Tahoma,Helvetica,Arial,SimSun,sans-serif;margin:50px auto;border:1px solid #eee;max-width:100%;background:#fff repeating-linear-gradient(-45deg,#fff,#fff 1.125rem,transparent 1.125rem,transparent 2.25rem);box-shadow:0 1px 5px rgba(0,0,0,.15)\";><div style=\";width:100%;background:#49BDAD;color:#fff;border-radius:10px 10px 0 0;background-image:-moz-linear-gradient(0deg,#43c6b8,#ffd1f4);background-image:-webkit-linear-gradient(0deg,#43c6b8,#ffd1f4);height:66px\";><p style=\";font-size:15px;word-break:break-all;padding:23px 32px;margin:0;background-color:hsla(0,0%,100%,.4);border-radius:10px 10px 0 0\";>尊敬的图书馆管理系统用户您好:</p></div><div style=\";margin:40px auto;width:90%\";><p>" + newpassword + "是您的新密码,请牢记</p><p>请注意:此邮件自动发送,请勿直接回复。</p></div></div></td></tr></tbody></table>";
                        request.AccountName = "你的发信域名";
                        request.FromAlias = "图书管理系统";
                        request.AddressType = 1;
                        request.TagName = "BackPassword";
                        request.ReplyToAddress = true;
                        request.ToAddress = (txtEmailReset.Text.Trim()).ToString();
                        request.Subject = "图书管理系统的重置后密码";
                        request.HtmlBody = pass;
                        SingleSendMailResponse httpResponse = client.GetAcsResponse(request);
                    }
                    finally
                    {
                        MessageBox.Show("邮件发送成功,如未收到请联系系统管理员或者在垃圾箱中寻找");
                    }
                    this.Close();
                }
                else
                {
                    MessageBox.Show("重置失败,请联系系统管理员!");
                }
            }
Last modification:February 23rd, 2019 at 01:24 pm

Leave a Comment