Hack Forums
[.NET] Email with attachment in Vb.net - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] Email with attachment in Vb.net (/showthread.php?tid=4868912)



Email with attachment in Vb.net - Strintent - Jun 17, 2015

I know most people know how to do this but i looked for a way to do it when i first started vb so im posting it here.

How to email with an attachment:
Code
Imports System.Net.Mail
Gmail:
Code
Dim Mail As New MailMessage
      Mail.Subject = "Subject"
      Mail.To.Add("To email")
      Mail.From = New MailAddress("Your email")
      Mail.Body = ("Main text")

      Dim attachment As System.Net.Mail.Attachment
      attachment = New System.Net.Mail.Attachment("Path to file")
      Mail.Attachments.Add(attachment)

      Dim SMTP As New SmtpClient("smtp.gmail.com")
      SMTP.EnableSsl = True
      SMTP.Credentials = New System.Net.NetworkCredential("Username", "Password")
      SMTP.Port = "587"
      SMTP.Send(Mail)

Hotmail/Live:
Code
Dim Mail As New MailMessage
      Mail.Subject = "Subject"
      Mail.To.Add("To email")
      Mail.From = New MailAddress("Your email")
      Mail.Body = ("Main text")

      Dim attachment As System.Net.Mail.Attachment
      attachment = New System.Net.Mail.Attachment("Path to file")
      Mail.Attachments.Add(attachment)

      Dim SMTP As New SmtpClient("smtp.Live.com")
      SMTP.EnableSsl = True
      SMTP.Credentials = New System.Net.NetworkCredential("Username", "Password")
      SMTP.Port = "587"
      SMTP.Send(Mail)



* - Only3 - Jun 17, 2015

3 TO 1 Mail.Attachments.Add(New System.Net.Mail.Attachment("Path to file"))
^^
Dim attachment As System.Net.Mail.Attachment
attachment = New System.Net.Mail.Attachment("Path to file")
Mail.Attachments.Add(attachment)


* - Strintent - Jun 17, 2015

Never knew that :) thanks


* - Odus - Jun 17, 2015

https://msdn.microsoft.com/en-us/library/7f38zh8x.aspx

Both of you!


* - nuk - Jun 17, 2015

I remember having an issue with GMail back in the day doing something like this... This will help people I'm sure so thanks for the share ;)


* - Strintent - Jun 18, 2015

Nice hope I can help :) I had the same problem


* - He B TeMy - Jun 18, 2015

Assigning a string to an int property, and you're releasing a source? u kiddin?


* - Strintent - Jun 18, 2015

Well if it works it works and it does so no im not kidding


* - Odus - Jun 18, 2015

(Jun 18, 2015 12:54 PM)Strintent Wrote:  Well if it works it works and it does so no im not kidding

It won't compile with Option Strict on. He does have a point though. The port property is expecting type integer yet you're initializing with a string.


* - Strintent - Jun 18, 2015

How would i change this then?


* - Odus - Jun 18, 2015

(Jun 18, 2015 01:01 PM)Strintent Wrote:  How would i change this then?

Remove the quotation marks. It's that easy!


* - Strintent - Jun 18, 2015

Oh okay makes sense thanks :)


Got to wait 24 hours to update the thread :|


* - GreyPhantom - Jun 20, 2015

This is a SMTP Email modded version. XD