ASP.NET:发送带有附件的电子邮件 [英] ASP.NET: Sending Email with Attachment

查看:100
本文介绍了ASP.NET:发送带有附件的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个联系我们"页面,该页面使用户能够发送带有附件的电子邮件.我编写了以下代码,但我不知道为什么它对我不起作用.在添加FileUpload之前它可以正常工作,但是在将其添加到表单之后,它却无法正常工作.

I am trying to develop a Contact Us page which enables the user to send emails with attachment. I wrote the following code but I don''t know why it doesn''t work with me. It was working before adding the FileUpload, but after adding it to the form, it did not work.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using System.Net;
using System.Text;


public partial class Contact : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            MultiView1.SetActiveView(ViewForm);
        }

    }



    protected void btnSend_Click(object sender, ImageClickEventArgs e)
    {
        SmtpClient sc = new SmtpClient("Mail Server");
        StringBuilder sb = new StringBuilder();
        MailMessage msg = null;

        sb.Append("Message from: " + txtName.Text + "\n");
        sb.Append("Email: " + txtEmail.Text + "\n");
        sb.Append("Message   : " + txtMessage.Text + "\n");
        
        //Attach file using FileUpload Control and put the file in memory stream
        if (FileUpload1.HasFile)
        {
            msg.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
        }

        try
        {
            msg = new MailMessage(txtEmail.Text,
                "Receiver Email", "Email Title",
                sb.ToString());

            sc.Send(msg);
            MultiView1.SetActiveView(ViewConfirm);
        }
        catch (Exception ex)
        {
            throw ex;
            // something bad happened
            //Response.Write("Something bad happened!");

        }
        finally
        { 

            if (msg != null)
            {
                msg.Dispose();
            }

        }
    }

}

推荐答案

您必须设置有效的邮件服务器地址,而不是邮件服务器".

查看此文档:
http://msdn.microsoft.com/en-us/library/system. net.mail.smtpclient.aspx [ ^ ]
You have to set a valid mail server address instead of "Mail Server".

Look at this documentation :
http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx[^]


尝试一下
MailMessage mail = new MailMessage();
mail.To ="me@mycompany.com;him@hiscompany.com;her@hercompany.com";
mail.From = "you@yourcompany.com";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body.";
MailAttachment attachment = new MailAttachment( Server.MapPath( "test.txt" ) ); 
mail.Attachments.Add( attachment );	//add the attachment
SmtpMail.SmtpServer = "smtp.gmail.com";  //your real server goes hereSmtpMail.Send( mail );


这篇关于ASP.NET:发送带有附件的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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