2015-07-14 5 views
0

Что я хочу сделать, это отправить электронное письмо с помощью кнопки с прикрепленным изображением. У меня этот код из моей страницы 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 (строка, строка) устарел.

Любая идея, что это и как ее решить? Спасибо!

+1

http://stackoverflow.com/questions/14193246/difference-between-clientscript-registerstartupscripttypeofpage-script-scri –

+1

Я хотел бы представить вам мой друг ... [Google] (https://www.google.com/search?q=registerstartupscript+obsolete) – Amit

ответ

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

Я пробовал это, но не отправлял. я имею в виду, что ничего не происходит после нажатия кнопки sir –

+0

Какую ошибку вы получаете? –

+0

без ошибок. он ничего не делает –

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

ничего не происходит, sir –

+0

ли это показывать предупреждение для отправки или отправки по электронной почте? что-нибудь?? – Jahangeer

+0

no sir ничего не случилось :( –

Смежные вопросы