发送带有按钮的电子邮件时出错

本文关键字:电子邮件 出错 按钮 | 更新日期: 2023-09-27 18:02:36

我想做的是通过一个按钮点击发送电子邮件附带的图像与它。我从我的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(string,string)已过时。

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

发送带有按钮的电子邮件时出错

try this

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);
        }
    }

你的电子邮件应该降低安全性。Gmail安全

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);
        }`