C#实现发送邮件的方法
程序员文章站
2023-12-11 18:20:34
本文实例为大家分享了android九宫格图片展示的具体代码,供大家参考,具体内容如下
#region 发送邮件部分
private static str...
本文实例为大家分享了android九宫格图片展示的具体代码,供大家参考,具体内容如下
#region 发送邮件部分 private static string frommail = "1111@126.com"; ///邮箱名称 private static string mailpwd = "111111"; ///密码 private static string tomail = "2222@163.com"; ///邮箱服务器 private static string filestr;//当前图片路径,在添加附件时用 /// <summary> /// 发送邮件 /// </summary> /// <param name="fileurl">附件地址,以~分</param> /// <param name="screen">是否发送截屏</param> /// <returns></returns> public static string sendmail(string fileurl, string screen) { mailaddress from = new mailaddress(frommail); mailaddress to = new mailaddress(tomail); mailmessage message = new mailmessage(from, to); message.subject = "m邮件 " +11111;//主题 message.subjectencoding = system.text.encoding.utf8; attachment attachfile = new attachment(filestr); if (screen == "true") message.attachments.add(attachfile); string[] files = fileurl.split('~'); for (int i = 0; i < files.length; i++) { if (file.exists(files[i])) { attachment attachfile1 = new attachment(fileurl); message.attachments.add(attachfile1); } } try { smtpclient client = new smtpclient("smtp." + from.host); sendmail(client, from, mailpwd, to, message); return "发送邮件成功!包含附件:" + fileurl + " 含截图?" + screen + " " + datetime.now.tostring(); } catch (smtpexception ex) { //如果错误原因是没有找到服务器,则尝试不加smtp.前缀的服务器 if (ex.statuscode == smtpstatuscode.generalfailure) { try { //有些邮件服务器不加smtp.前缀 smtpclient client = new smtpclient(from.host); sendmail(client, from, mailpwd, to, message); return "发送邮件成功!包含附件:" + fileurl + " 含截图?" + screen + " " + datetime.now.tostring(); } catch (smtpexception err) { return "发送邮件失败!" + err.message + " " + datetime.now.tostring(); } } else { return "发送邮件失败!" + ex.message + " " + datetime.now.tostring(); } } } //根据指定的参数发送邮件 private static void sendmail(smtpclient client, mailaddress from, string password, mailaddress to, mailmessage message) { //不使用默认凭证,注意此句必须放在client.credentials的上面 client.usedefaultcredentials = false; //指定用户名、密码 client.credentials = new networkcredential(from.address, password); //邮件通过网络发送到服务器 client.deliverymethod = smtpdeliverymethod.network; try { client.send(message); } catch { throw; } finally { //及时释放占用的资源 message.dispose(); } } #endregion
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。