发送邮件脚本asp.net用c# [英] Send mail script in asp.net with c#

查看:108
本文介绍了发送邮件脚本asp.net用c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请建议我一个code对于发送邮件在asp.net脚本的 C#。我想建立的查询表格在这里我想派我的电子邮件标识与附件,主题和正文的所有信息。


解决方案

 尝试
{
    //分配发件人,收件人,如有新邮件
    MailAddress发件人=
        新的MailAddress(sender@johnny$c$cr.com,发件人);    MailAddress收件人=
        新的MailAddress(recipient@johnny$c$cr.com,收件人);    MAILMESSAGE M = MAILMESSAGE新(发件人,收件人);
    m.Subject =测试信息;    //定义纯文本不同的观点,并加入到消息
    串plainTextBody =
        你必须使用支持HTML邮件的电子邮件客户端    AlternateView plainTextView =
        AlternateView.CreateAlternateViewFromString(
            plainTextBody,空,MediaTypeNames.Text.Plain);    m.AlternateViews.Add(plainTextView);    //定义嵌入图片的HTML不同的观点和
    //添加到消息。要引用附件图片为链接
    从你的HTML邮件正文//资源,用CID:内容识别
    //在< IMG>标签...
    串htmlBody =
        < HTML和GT;<身体GT;< H1>图片和LT; / H1>< BR>中+
        < IMG SRC = \\CID:SampleImage \\>< /身体GT;< / HTML>中;    AlternateView htmlView =
        AlternateView.CreateAlternateViewFromString(
            htmlBody,空,MediaTypeNames.Text.Html);    // ...然后定义实际LinkedResource匹配
    //为图像标记发现内容识别属性。在这种情况下,
    // HTML邮件包括标签
    //< IMG SRC = \\CID:SampleImage \\>和下面的
    // LinkedResource.ContentId设为SampleImage
    LinkedResource sampleImage =
        新LinkedResource(sample.jpg
            MediaTypeNames.Image.Jpeg);
    sampleImage.ContentId =SampleImage;    htmlView.LinkedResources.Add(sampleImage);    m.AlternateViews.Add(htmlView);    //最后,配置SMTP或交替使用
    // system.net mailSettings
    SmtpClient SMTP =新SmtpClient
          {
              主机=smtp.bigcompany.com
              UseDefaultCredentials =假,
              证书=
                  新的NetworkCredential(用户名,密码)
          };    //<system.net>
    //&LT; mailSettings&GT;
    //&LT; SMTP deliveryMethod =网络&GT;
    //&lt;网络主机=smtp.bigcompany.com
    //端口=25的DefaultCredentials =真/&GT;
    //&LT; / SMTP&GT;
    //&LT; / mailSettings&GT;
    //</system.net>    smtp.Send(米);
}
赶上(ArgumentException的)
{
    抛出新
        ArgumentException的(未定义发件人和/或收件人。);
}
赶上(FormatException)
{
    抛出新
        FormatException(无效的发件人和/或收件人。);
}
赶上(InvalidOperationException异常)
{
    抛出新
        InvalidOperationException异常(未定义SMTP服务器。);
}
赶上(SmtpFailedRecipientException)
{
    抛出新SmtpFailedRecipientException(
        邮件服务器说有收件人没有邮箱);
}
赶上(SmtpException前)
{
    //主机名无效导致引发WebException的InnerException是
    //提供了一个更具描述性的错误,所以得到的基异常
    例外内= ex.GetBaseException();
    抛出新SmtpException(无法发送消息:+ inner.Message);
}

Please suggest me a code for send mail script in asp.net with C#. I want to build inquiry form where i wanna send all information on my email Id with attachment, subject and body.

解决方案

try
{
    // Assign a sender, recipient and subject to new mail message
    MailAddress sender =
        new MailAddress("sender@johnnycoder.com", "Sender");

    MailAddress recipient =
        new MailAddress("recipient@johnnycoder.com", "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);
}

这篇关于发送邮件脚本asp.net用c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆