用c#在asp.net中发送邮件脚本

2024-03-26

请建议我一个代码发送邮件asp.net 中的脚本C#。我想建造调查表我想发送我的电子邮件 ID 上的所有信息,包括附件、主题和正文。


try
{
    // Assign a sender, recipient and subject to new mail message
    MailAddress sender =
        new MailAddress("[email protected] /cdn-cgi/l/email-protection", "Sender");

    MailAddress recipient =
        new MailAddress("[email protected] /cdn-cgi/l/email-protection", "Recipient");

    MailMessage m = new MailMessage(sender, recipient);
    m.Subject = "Test Message";

    // Define the plain text alternate view and add to message
    string plainTextBody =
        "You must use an email client that supports HTML messages";

    AlternateView plainTextView =
        AlternateView.CreateAlternateViewFromString(
            plainTextBody, null, MediaTypeNames.Text.Plain);

    m.AlternateViews.Add(plainTextView);

    // Define the html alternate view with embedded image and
    // add to message. To reference images attached as linked
    // resources from your HTML message body, use "cid:contentID"
    // in the <img> tag...
    string htmlBody =
        "<html><body><h1>Picture</h1><br>" +
        "<img src=\"cid:SampleImage\"></body></html>";

    AlternateView htmlView =
        AlternateView.CreateAlternateViewFromString(
            htmlBody, null, MediaTypeNames.Text.Html);

    // ...and then define the actual LinkedResource matching the
    // ContentID property as found in the image tag. In this case,
    // the HTML message includes the tag
    // <img src=\"cid:SampleImage\"> and the following
    // LinkedResource.ContentId is set to "SampleImage"
    LinkedResource sampleImage =
        new LinkedResource("sample.jpg",
            MediaTypeNames.Image.Jpeg);
    sampleImage.ContentId = "SampleImage";

    htmlView.LinkedResources.Add(sampleImage);

    m.AlternateViews.Add(htmlView);

    // Finally, configure smtp or alternatively use the
    // system.net mailSettings
    SmtpClient smtp = new SmtpClient
          {
              Host = "smtp.bigcompany.com",
              UseDefaultCredentials = false,
              Credentials =
                  new NetworkCredential("username", "password")
          };

    //<system.net>
    //    <mailSettings>
    //        <smtp deliveryMethod="Network">
    //            <network host="smtp.bigcompany.com"
    //              port="25" defaultCredentials="true"/>
    //        </smtp>
    //    </mailSettings>
    //</system.net>

    smtp.Send(m);
}
catch (ArgumentException)
{
    throw new
        ArgumentException("Undefined sender and/or recipient.");
}
catch (FormatException)
{
    throw new
        FormatException("Invalid sender and/or recipient.");
}
catch (InvalidOperationException)
{
    throw new
        InvalidOperationException("Undefined SMTP server.");
}
catch (SmtpFailedRecipientException)
{
    throw new SmtpFailedRecipientException(
        "The mail server says that there is no mailbox for recipient");
}
catch (SmtpException ex)
{
    // Invalid hostnames result in a WebException InnerException that
    // provides a more descriptive error, so get the base exception
    Exception inner = ex.GetBaseException();
    throw new SmtpException("Could not send message: " + inner.Message);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

用c#在asp.net中发送邮件脚本 的相关文章

随机推荐

  • 如何使用 PowerShell 将“date-1”格式设置为 mm-dd-yyyy?

    一个人如何得到date 1并在 PowerShell 中将其格式化为 mm dd yyyy 示例 如果今天是 2013 年 11 月 1 日 我的代码中需要 10 31 2013 我以前使用过 AddDays 1 但我似乎无法让它与任何格式
  • 启动 PyQt 应用程序而不阻塞主线程

    我正在向现有应用程序添加 PyQT UI 我必须初始化QAppliaction通过我在主线程上从该应用程序收到的回调 我无法从回调中同步执行此操作 因为我最终必须调用app exec 这是阻塞的 阻止现有应用程序继续运行 显然 生成常规线程
  • 将 JSON 数组读入类似 Julia DataFrame 的类型

    给定一个 JSON 文件 JSON 包会愉快地解析它 但如果我想把它作为DataFrame 或任何其他柱状数据结构 获得它的好方法是什么 目前 例如 我有 using JSON using DataFrames json str color
  • 使用 powershell 从多台计算机中提取注册表值

    我和一位同事正在尝试创建一个 powershell 脚本 该脚本使用一个 CSV 文件 其中包含公司网络上所有计算机的名称 并使用这些名称连接到远程注册表并提取特定值 这是我们目前所拥有的 strMachineName import csv
  • MySQL存储过程中用于选择数据的if语句

    我有下面的 SQL 代码 DELIMITER CREATE PROCEDURE test new procedure queryString VARCHAR 255 BEGIN SELECT tempValue COUNT FROM tes
  • 被类覆盖的扩展方法不会给出警告

    我在另一个线程中进行了讨论 发现类方法优先于具有相同名称和参数的扩展方法 这很好 因为扩展方法不会劫持方法 但假设您已向第三方库添加了一些扩展方法 public class ThirdParty public static class Th
  • pdf-writer 无效的多字节字符 (US-ASCII) 期望 keywords_end 内容 = "%PDF-#{@version}\n%âãÏÓ\n" Rails 3

    我在尝试运行的协作项目中遇到了 pdf writer gem 的问题 我正在使用RVM Ruby 1 9 2p180 Rails 3 1 0 on Ubuntu 10 10 我创建了一个新的gemset使用 Bundler 安装所需的 ge
  • 如何模拟 Object.getClass?

    我正在开发一个 Java 项目 想要为 DTO 中的 equals 方法编写单元测试 在 equals 方法中 有一个 getClass 方法由两个被测试对象调用 我想模拟这个 但我不知道它想要什么类型的对象 我试过 when mockRo
  • 带有窗口服务的网络打印

    我有一个朋友用窗口窗体做了同样的事情 我需要的是将其作为窗口服务 Window 服务程序 打印控制器服务 将用作中央打印机控制器 根据情况 我将打印作业发送到网络上的不同打印机 打印机安装在USB上 已经可以进行测试打印 但是当我列出已安装
  • HttpContext.Current.User.Identity.Name 始终为 string.Empty

    您好 我使用自定义 MembershipProvider 我想在应用程序场景中知道当前用户名 但是当我尝试访问 HttpContext Current User Identity Name 时 它 总是返回 string Empty if
  • 限制事件队列中的事件

    In a 最近的问题 https stackoverflow com questions 36492268 nodejs running out of memory processing csv files 我概述了在处理大量 csv 文件
  • 用于搜索某个单词/字符串是否包含在实体字段之一的 JPQL 查询

    基本上 这类似于查看句子中是否存在某个单词 有实体帖子 public class Post implements Serializable Id GeneratedValue strategy IDENTITY Column name po
  • 将公式向下填充到列中的最后一行

    我试图将单元格 M3 中的公式绘制到数据集的末尾 我使用 L 列作为基础来确定最后一个包含数据的单元格 我的公式是两个单元格的串联 它们之间有一个文本逗号 我的公式是 G3 L3 我希望Excel能画出这个公式 单元格 M4 将是 G4 L
  • 注释 Lambda 表达式的函数接口

    Java 8 引入了两者拉姆达表达式 http docs oracle com javase tutorial java javaOO lambdaexpressions html and 类型注释 http docs oracle com
  • 正斜杠还是反斜杠?

    我希望分别向与我的程序不同的目录写入和读取文本文件 当我指定要写入或读取的目录时 是否应该使用正斜杠或反斜杠来标识文件路径 使用正斜杠将使其与系统无关 为了简单起见 我会坚持这一点 考虑使用java io File separator ht
  • 如果在 JQuery 中未选中所有子节点,则取消选中父节点

    如果所有子节点都未选中 我想取消选中父节点 ul li li ul
  • 判断服务器是否支持resume get请求

    如何确定服务器是否支持恢复文件传输或获取请求 我的想法是设置标头以在字节 2 而不是 0 处启动 get 请求 如果给出正确的结果 则立即关闭 http 请求 但我想知道服务器对另一种探针的响应是否会向我透露此信息 要探测服务器的下载恢复功
  • VB.Net Excel Automation如何处理敏感度标签

    当我在 VB Net 中为用户创建 Excel 文件时 当我尝试保存文件时出现错误 当文件为只读时 您无法执行此操作 并且似乎是因为尚未选择敏感度标签 我无法找到任何有关在执行 Excel 自动化时处理敏感度标签的文档 因此我想知道是否有人
  • 服务器端处理的数据表上的“列索引”

    有谁知道如何添加列索引 to a 服务器端处理数据表 http www datatables net 基本上喜欢http www datatables net examples api counter columns html http w
  • 用c#在asp.net中发送邮件脚本

    请建议我一个代码发送邮件asp net 中的脚本C 我想建造调查表我想发送我的电子邮件 ID 上的所有信息 包括附件 主题和正文 try Assign a sender recipient and subject to new mail m