使用按钮发送电子邮件时出错

时间:2015-07-14 06:00:28

标签: c# email button

我想要做的是通过点击按钮发送电子邮件,附带图片。我从我的aspx页面获得了这段代码:

<div>
<table style=" border:1px solid" align="center">
<tr>
<td colspan="2" align="center">
<b>Send Mail using gmail credentials in asp.net</b>
</td>
</tr>
<tr>
<td>
Gmail Username:
</td>
<td>
<asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Gmail Password:
</td>
<td>
<asp:TextBox ID="txtpwd" runat="server" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Subject:
</td>
<td>
<asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
To:
</td>
<td>
<asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Attach a file:
</td>
<td>
<asp:FileUpload ID="fileUpload1" runat="server" />
</td>
</tr>
<tr>
<td valign="top">
Body:
</td>
<td>
<asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Columns="30" Rows="10" ></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnSubmit" Text="Send" runat="server" onclick="btnSubmit_Click" />
</td>
</tr>
</table>
</div>

这是背后的代码:

try
{
    MailMessage Msg = new MailMessage();
    // Sender e-mail address.
    Msg.From = new MailAddress(txtUsername.Text);
    // Recipient e-mail address.
    Msg.To.Add(txtTo.Text);
    Msg.Subject = txtSubject.Text;
    // File Upload path
    String FileName = fileUpload1.PostedFile.FileName;
    string mailbody = txtBody.Text + "<br/><img src=cid:companylogo>";
    LinkedResource myimage = new LinkedResource(FileName);
    // Create HTML view
    AlternateView htmlMail = AlternateView.CreateAlternateViewFromString(mailbody, null, "text/html");
    // Set ContentId property. Value of ContentId property must be the same as
    // the src attribute of image tag in email body. 
    myimage.ContentId = "companylogo";
    htmlMail.LinkedResources.Add(myimage);
    Msg.AlternateViews.Add(htmlMail);
    // your remote SMTP server IP.
    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.gmail.com";
    smtp.Port = 587;
    smtp.Credentials = new System.Net.NetworkCredential(txtUsername.Text, txtpwd.Text);
    smtp.EnableSsl = true;
    smtp.Send(Msg);
    Msg = null;
    Page.RegisterStartupScript("UserMsg", "<script>alert('Mail sent thank you...');if(alert){ window.location='SendMail.aspx';}</script>");
}
catch (Exception ex)
{
    Console.WriteLine("{0} Exception caught.", ex);
}

但行

Page.RegisterStartupScript("UserMsg", "<script>alert('Mail sent thank you...');if(alert){ window.location='SendMail.aspx';}</script>");

给我一​​个错误说:

  

RegisterStartupScript(字符串,字符串)已过时。

知道这是什么以及如何解决它?谢谢!

2 个答案:

答案 0 :(得分:0)

您的电子邮件应该具有较低的安全性。 Gmail Security

try
        {
            MailMessage Msg = new MailMessage();
            // Sender e-mail address.
            Msg.From = new MailAddress(txtUsername.Text);
            // Recipient e-mail address.
            Msg.To.Add(txtTo.Text);
            Msg.Subject = txtSubject.Text;
            Msg.IsBodyHtml = true;
            // File Upload path
            if (fileUpload1.HasFile)
            {
                // Create HTML view
                string mailbody = txtBody.Text + "<br/><img src=cid:companylogo>";
                AlternateView htmlMail = AlternateView.CreateAlternateViewFromString(mailbody, null, "text/html");
                string path = Server.MapPath("~/images/");
                string FileName =path+( fileUpload1.PostedFile.FileName);
                //Msg.Attachments.Add(new Attachment(fileUpload1.PostedFile.InputStream, FileName));
                LinkedResource myimage = new LinkedResource(FileName);
                myimage.ContentId = "companylogo";
                htmlMail.LinkedResources.Add(myimage);
               Msg.AlternateViews.Add(htmlMail);
            }

            // your remote SMTP server IP.
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.Credentials = new System.Net.NetworkCredential(txtUsername.Text, txtpwd.Text);
            smtp.EnableSsl = true;
            smtp.Send(Msg);
            Msg = null;
            Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", "alert('Mail sent thank you...');if(alert){ window.location='WebForm1.aspx';}", true);
        }
        catch (Exception ex)
        {
            Console.WriteLine("{0} Exception caught.", ex);
        }`

答案 1 :(得分:0)

试试这个

using System;
using System.Net.Mail;
using System.IO;

 protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            MailMessage Msg = new MailMessage();
            // Sender e-mail address.
            Msg.From = new MailAddress(txtUsername.Text);
            // Recipient e-mail address.
            Msg.To.Add(txtTo.Text);
            Msg.Subject = txtSubject.Text;
            // File Upload path
            String FileName = fileUpload1.PostedFile.FileName;
            string mailbody = txtBody.Text + "<br/><img          src=cid:companylogo>";
            string fileName = Path.GetFileName(FileName);
            Msg.Attachments.Add(new Attachment(fileUpload1.PostedFile.InputStream, fileName));
            //LinkedResource myimage = new LinkedResource(FileName);
            // Create HTML view
            AlternateView htmlMail = AlternateView.CreateAlternateViewFromString(mailbody, null, "text/html");
            // Set ContentId property. Value of ContentId property must be the same as
            // the src attribute of image tag in email body. 
            //myimage.ContentId = "companylogo";
           // htmlMail.LinkedResources.Add(myimage);
            Msg.AlternateViews.Add(htmlMail);
            // your remote SMTP server IP.
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.Credentials = new System.Net.NetworkCredential(txtUsername.Text, txtpwd.Text);
            smtp.EnableSsl = true;
            smtp.Send(Msg);

            Msg = null;
            //ClientScript.RegisterStartupScript( "<script>alert('Mail sent thank you...');if(alert){ window.location='SendMail.aspx';}</script>");
            Response.Write("<script>alert('Email Sent');</script>"); 

        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('Unable To Send Email');</script>"); 
            Console.WriteLine("{0} Exception caught.", ex);
        }
    }