Answered by:
Attach File from Web Form

Question
-
User-401818107 posted
Hi expert, I have a web form. On the form , the users need to fill out the info (previously works) and attach the file (Want to add this new feature) and send out. Could someone give me a simple example of how to attach the file and send out. Thank you!
Sunday, December 9, 2018 4:29 AM
Answers
-
User283571144 posted
Hi Jessy,
Of course.
VB.NET Codes:
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Dim smtpClient As SmtpClient = New SmtpClient("smtp.gmail.com", 587) smtpClient.Credentials = New System.Net.NetworkCredential("aaaa@gmail.com", " ") smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network smtpClient.EnableSsl = True Dim mail As MailMessage = New MailMessage() mail.From = New MailAddress("aaa@gmail.com.com") mail.[To].Add(New MailAddress("v-aaa@hotmail.com")) Dim fname As String = FileUpload1.PostedFile.FileName mail.Attachments.Add(New Attachment(FileUpload1.PostedFile.InputStream, fname)) Try smtpClient.Send(mail) Response.Write("send email successfully") Catch [error] As Exception Response.Write("fail in send" & [error].Message) End Try End Sub
Best Regards,
Brando
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, December 14, 2018 8:17 AM
All replies
-
User283571144 posted
Hi Jessy,
If you want to know how to send the email with the attenchment file?
If this is your requirement,I suggest you could use FileUpload control to get file and use SmtpClient class and MailMessage's Attachment class to realize sending e-mail requirement.
Notice: Different mailboxes have different port numbers, such as google mailbox with a port number of 587.
More details, you could refer to below codes:
aspx:
<asp:FileUpload ID="FileUpload1" runat="server" /><br /> <asp:Button ID="Button2" runat="server" Text="send" OnClick="Button2_Click" />
Code-behind:
protected void Button2_Click(object sender, EventArgs e) { SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587); smtpClient.Credentials = new System.Net.NetworkCredential("AAA@gmail.com", ""); smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; smtpClient.EnableSsl = true; MailMessage mail = new MailMessage(); mail.From = new MailAddress("AAAA@gmail.com"); mail.To.Add(new MailAddress("AAAAA@hotmail.com")); string fname = FileUpload1.PostedFile.FileName; mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, fname)); try { smtpClient.Send(mail); Response.Write("send email successfully"); } catch (Exception error) { Response.Write("fail in send" + error.Message); } } }
Best Regards,
Brando
Monday, December 10, 2018 1:11 PM -
User-401818107 posted
Could you convert the coding behind into the vb.net? Thank you!
Friday, December 14, 2018 4:29 AM -
User283571144 posted
Hi Jessy,
Of course.
VB.NET Codes:
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Dim smtpClient As SmtpClient = New SmtpClient("smtp.gmail.com", 587) smtpClient.Credentials = New System.Net.NetworkCredential("aaaa@gmail.com", " ") smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network smtpClient.EnableSsl = True Dim mail As MailMessage = New MailMessage() mail.From = New MailAddress("aaa@gmail.com.com") mail.[To].Add(New MailAddress("v-aaa@hotmail.com")) Dim fname As String = FileUpload1.PostedFile.FileName mail.Attachments.Add(New Attachment(FileUpload1.PostedFile.InputStream, fname)) Try smtpClient.Send(mail) Response.Write("send email successfully") Catch [error] As Exception Response.Write("fail in send" & [error].Message) End Try End Sub
Best Regards,
Brando
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, December 14, 2018 8:17 AM