how to send an email in vb.net

liunx

Guest
this is what i have, please help
{


MailAddress to = new MailAddress("[email protected]", "Joe Public");

MailAddress from = new MailAddress("[email protected]", "Plane Jane")



MailMessage MailMsgObj = new MailMessage();

MailMsgObj.To.Add(to);

MailMsgObj.From = from;

MailMsgObj.Subject = "Hello there!";

MailMsgObj.Body = "Greetings, blah blah blah";


MailMsgObj.IsBodyHtml = false;

SmtpClient mailClient = new SmtpClient();

mailClient.Host = "localhost";

mailClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;

mailClient.Send(MailMsgObj); // sending msg created

}

catch (SmtpException ex)

{

throw new ApplicationException

("SmtpException has occured: " + ex.Message);

}Hi,

This is a stripped down version of what I use in VB.NET. You can add your own email address validation, disclaimer text, error trapping etc etc.

Code behind:

Private Sub lbtnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtnSend.Click

'send email
Dim objEmail As New clsEmail
Dim strErrorSendingEmail As String
Dim strFrom As String = ""
Dim strTo As String = ""
Dim strCC As String = ""
Dim strSubject As String = ""
Dim strBody As String = ""

If txtFeedbackComment.Text = "" Then
'reset validation labels to show error and return to email form
lblFeedbackError.Text = "* Required"
Exit Sub
Else
lblFeedbackError.Text = "*"
End If

'Send email

strFrom = txtFeedbackEmail.Text

strTo = "whatever email address"

strSubject = "whatever subject"

strBody = txtFeedbackEmail.Text

plhFeedback.Visible = False
plhFeedbackSent.Visible = True
lblFeedbackHeader.Visible = False

If objEmail.SendMail(strTo, strCC, strFrom, strBody, strSubject) Then

lblFeedbackSent.Text = "Thank you for your feedback."

Else

lblFeedbackSent.Text = "Unfortunately, there was an error sending your feedback. The website administrator has been informed."

End If

End Sub

Email class:

Public Function SendMail(ByVal strTo As String, ByVal strCC As String, ByVal strFrom As String, ByVal strBody As String, ByVal strSubject As String) As Boolean

Dim objMM As New System.Web.Mail.MailMessage

objMM.From = strFrom
objMM.To = strTo
objMM.Cc = strCC
objMM.Subject = strSubject
objMM.BodyFormat = System.Web.Mail.MailFormat.Html
objMM.Body = strBody

System.Web.Mail.SmtpMail.SmtpServer = 'LIVE SERVER SMTP NAME

Try
SmtpMail.Send(objMM)

Catch ex As Exception

'Return error to the user and log error in log file
Return False

End Try

Return True

objMM = Nothing

End Function

Hope this helps.

Cheers

AJi am battling to use this code, its looking for clsEmail and a lot of other things.

Please sheck the following:
have been trying to send an email from my asp.net apllication

this is one example i got from the net but its not working



Imports System.Net.Mail 'this is not reconised in my app

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Try
Dim message As New MailMessage(txtFrom.Text, txtTo.Text, txtSubject.Text, txtBody.Text)
Dim emailClient As New SmtpClient(txtSMTPServer.Text)
Dim SMTPUserInfo As New System.Net.NetworkCredential(txtSMTPUser.Text, txtSMTPPass.Text)
emailClient.UseDefaultCredentials = False
emailClient.Credentials = SMTPUserInfo
emailClient.Send(message)
litStatus.Text = "Message Sent"
Catch ex As Exception
litStatus.Text = ex.ToString()
End Try
End Sub



The front end .aspx page looks something like this:

<table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#5482fc">
<tr>
<td height="50" align="center" class="lgHeader1">How to Send Email with Authentication using ASP.NET 2.0 and C#</td>
</tr>
</table>
<br />
<table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#cccccc">
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1"> To</td>
<td bgcolor="#FFFFFF"><asp:TextBox ID="txtTo" runat="server" Columns="50"></asp:TextBox></td>
</tr>
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1"> From</td>
<td bgcolor="#FFFFFF"><asp:TextBox ID="txtFrom" runat="server" Columns="50"></asp:TextBox></td>
</tr>
<tr>
<td align="right" bgcolor="#eeeeee" class="header1"> SMTP Server</td>
<td bgcolor="#FFFFFF"><asp:TextBox ID="txtSMTPServer" runat="server" Columns="50"></asp:TextBox></td>
</tr>
<tr>
<td align="right" bgcolor="#eeeeee" class="header1">SMTP User</td>
<td bgcolor="#FFFFFF"><asp:TextBox ID="txtSMTPUser" runat="server" Columns="50"></asp:TextBox></td>
</tr>
<tr>
<td align="right" bgcolor="#eeeeee" class="header1">SMTP Pass</td>
<td bgcolor="#FFFFFF"><asp:TextBox ID="txtSMTPPass" runat="server" Columns="50"></asp:TextBox></td>
</tr>
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1"> Subject</td>
<td bgcolor="#FFFFFF"><asp:TextBox ID="txtSubject" runat="server" Columns="50"></asp:TextBox></td>
</tr>
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1"> Body</td>
<td bgcolor="#FFFFFF"><asp:TextBox ID="txtBody" runat="server" Columns="40" TextMode="MultiLine"></asp:TextBox></td>
</tr>
<tr>
<td align="right" bgcolor="#eeeeee" class="header1">Action</td>
<td bgcolor="#FFFFFF"><asp:Button ID="btnSubmit" runat="server" Text="Send Email" OnClick="btnSubmit_Click" /></td>
</tr>
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1">Status</td>
<td bgcolor="#FFFFFF" class="basix"><asp:Literal ID="litStatus" runat="server"></asp:Literal></td>
</tr>
</table>





The flow for the code behind page is as follows.

Imports System.Net.Mail

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Try
Dim message As New MailMessage(txtFrom.Text, txtTo.Text, txtSubject.Text, txtBody.Text)
Dim emailClient As New SmtpClient(txtSMTPServer.Text)
Dim SMTPUserInfo As New System.Net.NetworkCredential(txtSMTPUser.Text, txtSMTPPass.Text)
emailClient.UseDefaultCredentials = False
emailClient.Credentials = SMTPUserInfo
emailClient.Send(message)
litStatus.Text = "Message Sent"
Catch ex As Exception
litStatus.Text = ex.ToString()
End Try
End Sub
End Class



can someone who know a simple way of doing this please help. I dont need to fill a form before sending, i just want to be able to send a "one liner" email
 
Top