Send an email using System.Web.mail

xiaoxiao2021-03-06  58

http://community.9cbs.net/expert/topic/3198/3198073.xml?temp=.3856623

Use System.Web.mail to send emails by the mail server you want to verify, the following is a class that Scott Water writes in dottext, which is more convenient to use, the code of the entire class is as follows:

Using system;

Using system.web.mail;

Namespace mytest

{

///

/// systemmail's summary description.

///

Public Class Systemmail

{

Private string _adminemail;

Private string _smtpserver = "localhost";

PRIVATE STRING _PASSWORD;

Private string _username;

Public systemmail ()

{

}

Public String Adminemail

{

Get {return_adminemail;

SET {_adminemail = value;}

}

Public String SMTPServer

{

Get {return _smtpserver;}

Set {_smtpserver = value;}

}

Public String Password

{

Get {return _password;

Set {_password = value;}

}

Public String Username

{

Get {return _username;}

Set {_username = value;

}

Public Bool Send (String to, String from, String Subject, String Message)

{

Try

{

MailMessage EM = New MailMessage ();

Em.to = TO;

Em.from = from;

Em.subject = Subject;

Em.body = message;

// Found Out How To Send Authenticated Email Via System.Web.mail At http://systemwebmail.com (Fact 3.8)

IF (this.username! = null& this.password! = null)

{

Em.fields.add ("http://schemas.microsoft.com/cdpo/configuration/smtpauthenticate", "1"); // Basic Authentication

Em.fields.add ("http://schemas.microsoft.com/cdpo/configuration/sendusername", this.username); // set your username here

Em.fields.add ("http://schemas.microsoft.com/cdo/configuration/sendpassword", this.password); // set your password here

}

SMTPMAIL.SMTPSERVER = this.smtpserver;

SMTPMAIL.SEND (EM);

Return True;

}

Catch

{

Return False;

}

}

}

}

Need more information You can view http://systemwebmail.com

Send mail in vb.net, asp.nethttp://www.freevbcode.com/showcode.asp? Id = 6401

Author: anonymous category: VB.Net/asp.net type: Snippets Difficulty: Beginning

Version Compatibility: Visual Basic.net

.............................. ...CRIPLILE, Hotel.

This Code Has Been Viewed 12092 Times.

INSTRUCTIONS: COPY The Declarations and Code Below and Paste Directly Into Your VB Project.

Declarations: Sending Mail Using Dot Net

The System.Web.Mail namespace provides the classes for sending Email in Dot net. MailMessage Manage the mail message contents. Properties Attachment Specifies the list of the attachments that are transmitted with the message Bcc A list of semicolon delimited email addresses that receive a Blind Carbon Copy of the message body Contains the message text that has to be sent. BodyEncoding The encoding type of the email message. BodyFormat Defines the content type of the body of the message Cc A list of semicolon delimited email addresses that receive a Carbon Copy of the message From The email address of the sender. Header Specifies the custom headers which are transmitted with the Message priority The priority of the email message Subject Subject Line of the email message. To email address of the recipient. MailAttachments Manage the mail attachment. SmtpMail Send Email to The Mail Server. Let us See it at Step Create a Visual Basic Application and Drop Following Cont rols and set the properties accordingly Control Property Label Text: Smtp Server TextBox Name: txtSMTPServer Label Text: From TextBox Name: txtFrom Label Text: From Display Name TextBox Name: txtFromDisplayName Label Text: Recipient TextBox txtTo Label Text: Attachment ListBox Name: lstAttachment Label Text: Subject TextBox Name: txtSubject Label Text: Message TextBox Name: txtMessage Multiline: True Scrollbars: Both Button Text: Add attachment Name: btnAdd Button Text: Remove attachment Name: btnRemove Button Text: Send Name: btnSend CheckBox Text: Send As HTML Name: Chkformat OpenFiledialog Name: OFD default: *. * InitialDirectory: C: / MultiSelectr:

true Now let us see the coding part Invoke the Code widow and type the following statement above the Class declarationImports System.Web.MailWithin the Class declaration, in the general section declare variables required for this project 'Variable which will send the mailDim obj As System .Web.Mail.SmtpMail 'Variable to store the attachments Dim Attachment As System.Web.Mail.MailAttachment' Variable to create the message to sendDim Mailmsg As New System.Web.Mail.MailMessage () Double click on the addattachment button to add the code Type the following lines' Show open dialogue box to select the files to attachDim Counter As IntegerOFD.CheckFileExists = TrueOFD.Title = "Select file (s) to attach" OFD.ShowDialog () For Counter = 0 to UBound (OFD. FileNames) lstAttachment.Items.Add (OFD.FileNames (Counter)) Next Double Click on the Removeattachment button Type the following lines' Remove the attachmentsIf lstAttachment.SelectedIndex> -1 ThenlstAttachment.Items.RemoveAt (lstAttachment.Sel ectedIndex) End If Double Click on the Send button Type the following lines Dim Counter As Integer 'Validate the dataIf txtSMTPServer.Text = "" ThenMsgBox ( "Enter the SMTP server info ... !!!", MsgBoxStyle.Information, "Send EXIT SUBEND IF TXTFROM.TEXT = "" THEN MSGBOX ("Enter the from email address ... !!!", msgboxstyle.information, "send email" EXIT SUBEND IF IF TXTTO.TEXT = "" "Thenmsgbox ("Enter the Recipient Email Address ... !!!", msgboxstyle.information, "send email") exit subend if if txtsubject.text = "" "" "" Enter the email subject ... !!! "

, MsgBoxStyle.Information, "Send Email") Exit SubEnd If 'Set the properties' A ssign the SMTP serverobj.SmtpServer = txtSMTPServer.Text'Multiple recepients can be specified using; as the delimeter' A ddress of the recipientMailmsg.To = txtTo.Text our From Address Ou Ou ou can also use a custom header Reply-To for a different replyto addressMailmsg.From = "/" & txtFromDisplayName.Text & "/ <" & txtFrom.Text & ">" 'Specify the body formatIf chkFormat. Checked = True ThenMailmsg.BodyFormat = MailFormat.Html 'Send the mail in HTML FormatElseMailmsg.BodyFormat = MailFormat.TextEnd If' If you want you can add a reply to header 'Mailmsg.Headers.Add ( "Reply-to", "Manoj @ geinetech.net ") 'custom headersare added like this'Mailmsg.Headers.Add (" Manoj "," TestHeader ")' A 'ashamed ail SubjectMailmsg.Subject = txtSubject.Text ttach the files one by oneFor Counter = 0 To lstAttachment.Items .Count - 1ttachment = new mailattachment (lstattachment.items (counter) 慉 DD it to the mail message mailmsg.attachments.add (ATT) achment) Next ashamed ail BodyMailmsg.Body = txtMessage.Text indignant all the send method to send the mailobj.Send (Mailmsg) This application is now ready to run, try it. If you have any queries mail it to manoj@geinetech.netSending Email with attachments (vb.net/asp.net)

http://www.freevbcode.com/showcode.asp?id=5486

Imports System.Web.mail

Imports system.io

'Two functions

'Same Except First Takes A String for Attachment

'Second Takes An Array List So You CAN Send Multiple

'Attachments

'From: Email Address from

'TO: Email Address to

'Subject: Subject; Body: MessageText'Optional CC, BCC: CC And BCC Recipients

'SMTPSERVER: OPTIONAL, IF NOT Specified

'local machine is buy

'AttachmentFile (First Function: Optional, File Name)

'Attachmentfiles (Second Function: Optional, List of

'attachments in form of an array list

Public Sub Sendmailongoneattachment (byval from as string, _

BYVAL Sendto As String, Byval Subject As String, _

Byval body as string, _

Optional byval attachmentfile as string = "", _

Optional byval cc as string = "", _

Optional byval bcc as string = "", _

Optional Byval SMTPSERVER AS STRING = "" "

Dim MyMessage as MailMessage

Try

MyMessage = new mailmessage ()

With mymessage

.To = sendto

.From = from

.Subject = SUBJECT

.BODY = BODY

.BodyFormat = mailformat.text

'Can user mailformat.html if you prefer

IF CC <> "" THEN .CC = CC

IF BCC <> "" Then .bcc = ""

If FileExists (attachmentfile) THEN _

.Attachments.add (attachmentfile)

End with

IF SMTPSERVER <> "" ""

SMTPMAIL.SMTPSERVER = SMTPSERVER

SMTPMAIL.SEND (MyMessage)

Catch myexp as Exception

Throw myexp

END TRY

End Sub

Public Sub SendmailmultiTtachments (byval from as string, _

BYVAL Sendto As String, Byval Subject As String, _

Byval body as string, _

Optional byVal attachmentfiles as arraylist = nothing, _

Optional byval cc as string = "", _

Optional byval bcc as string = "", _

Optional Byval SMTPSERVER AS STRING = "" "

Dim MyMessage as MailMessage

DIM I, ICNT AS INTEGER

Try

MyMessage = new mailmessage ()

With mymessage

.To = sendto

.From = from.subject = Subject

.BODY = BODY

.BodyFormat = mailformat.text

'Can user mailformat.html if you prefer

IF CC <> "" THEN .CC = CC

IF BCC <> "" Then .bcc = ""

IF not attachmentfiles is nothing then

ICNT = attachmentfiles.count - 1

For i = 0 to ICNT

IF FileExists (AttachmentFiles (i)) THEN _

.Attachments.add (attachmentfiles (i))

NEXT

END IF

End with

IF SMTPSERVER <> "" ""

SMTPMAIL.SMTPSERVER = SMTPSERVER

SMTPMAIL.SEND (MyMessage)

Catch myexp as Exception

Throw myexp

END TRY

End Sub

Private function fileexists (Byval FileFullPath as string)

As boolean

If Trim (FileFullPath = "" "THEN RETURN FALSE

DIM f as new IO.fileinfo (FileFullPath)

Return F.Exists

END FUNCTION

Check An Email Address for Validity (VB.Net and VB6)

Http://www.freevbcode.com/showcode.asp?id=4904

Function Validemail (Byval String As String) AS Boolean

'Created by Chad M. Kovac

'Tech Knowledge, Inc.

'http://www.techknowledgedgeyinc.com

DIM BCK As Boolean

DIM STRDOMAINTYPE AS STRING

DIM STRDOMAINNAME AS STRING

Const sinvalidchars as string = "! # $% ^ & * () = {} [] | /;: '/?>, <"

DIM I as integer

BCK = Not INSTR (1, Strcheck, Chr (34))> 0 'Check to See if The Is A Double Quote

IF not bck then goto exitfunction

BCK = Not INSTR (1, StrCheck, "..")> 0 'Check to See eti the is more consecutive dots

IF not bck then goto exitfunction

'Check for Invalid Characters.

If Len (STRCHECK)> LEN (SINVALIDCHARS) THEN

For i = 1 to Len (SINVALIDCHARS)

IF INSTR (Since (Sinvalidchars, I, 1)> 0 THEN

BCK = false

Goto EXITFUNCTION

End ifnext

Else

For i = 1 to Len (StrCheck)

IF INSTR (SINVALIDCHARS, MID (STRCHECK, I, 1)> 0 THEN

BCK = false

Goto EXITFUNCTION

END IF

NEXT

END IF

IF INSTR (1, Strcheck, "@")> 1 Then 'Check for an @ Symbol

BCK = LEN (Left (Strcheck, INSTR (1, Strcheck, "@") - 1))> 0

Else

BCK = false

END IF

IF not bck then goto exitfunction

Strcheck = Right (Strcheck, Len (StrCheck) - INSTR (1, Strcheck, "@"))))))

BCK = Not Instr (1, Strcheck, "@")> 0 'Check To See if The are Too Many @' s

IF not bck then goto exitfunction

STRDOMAINTYPE = Right (Strcheck, Len (StrCheck) - INSTR (1, Strcheck, "."))

BCK = LEN (STRDOMAINTYPE)> 0 and INSTR (1, StrCheck, ".")

IF not bck then goto exitfunction

Strcheck = Left (Strectyck, Len (StrDomainType) - 1)

Do Until INSTR (1, Strcheck, ".") <= 1

IF LEN (STRCHECK)> = INSTR (1, Strcheck, ".") THEN

Strcheck = Left (Strcheck, Len (Strcheck) - (INSTR (1, Strcheck, ".") - 1))

Else

BCK = false

Goto EXITFUNCTION

END IF

Loop

IF strcheck = "." Or len (strcheck) = 0 THEN BCK = FALSE

EXITFUNCTION:

Validemail = BCK

END FUNCTION

Public class mymail

Private _adminemail as string

Private _smtpserver as string

Private _password as string

Private_username as string

Public Sub New ()

End Sub 'NEW

Public property admin () AS STRING

Get

Return_adminemail

END GET

Set (byval value as string)

_adminemail = value

End set

End Property

Public property SMTPSERVER () AS STRING

Get

Return_smtpserver

END GET

Set (byval value as string)

_SMTPSERVER = VALUE

End set

End Property

Public property password () AS STRING

Get

Return _password

END GET

Set (byval value as string)

_password = value

End set

End Property

Public property username () AS STRING

Get

Return_username

END GET

Set (byval value as string)

_USERNAME = VALUE

End set

End Property

Public Function Send (Byval "AS String, Byval from As String, Byval Message As String) AS Boolean

Try

DIM EM AS New MailMessage

Em.to = [to]

Em.from = from

Em.subject = SUBJECT

Em.body = message

'Found Out How To Send Authenticated Email Via System.Web.mail At

http://systemwebmail.com (Fact 3.8)

IF not (username is nothing) and not (password is nothing) THEN

Em.fields.add ("

http://schemas.microsoft.com/cdpo/configuration/smtpauthenticate "," 1 ") 'Basic Authentication

Em.fields.add ("

http://schemas.microsoft.com/cdpo/configuration/sendusername ", me.username) 'set your username here

Em.fields.add ("

http://schemas.microsoft.com/cdo/configuration/sendpassword ", me.password) 'set your password here

END IF

SMTPMAIL.SMTPSERVER = SMTPSERVER

SMTPMAIL.SEND (EM)

Return True

Catch

END TRY

End function 'Send

End Class' Systemmail

转载请注明原文地址:https://www.9cbs.com/read-114605.html

New Post(0)