VB Code to Send Mail and Import Mail Ids

HI I am Attaching a file which is VB Code file.
By this file you will be able to import email addresses from xls file.
If you r file contains many words then this program will import only valid mail ids.
so no need to worry about the import.
now by importing the address you can send mail to these addresses.


Imports System.Text.RegularExpressions
Imports System.IO.File
Imports System.Collections.Generic
Imports System.IO
Imports SendMailToNewUser
Partial Class ImportsContact
Inherits System.Web.UI.Page
Private sr As StreamReader
Private strng As New StringBuilder()
' private void textBox1_PreviewKeyDown ( object sender, PreviewKeyDownEventArgs e )
'{
'if (e.KeyCode == Keys.Return)
'{
'Status.Text = "Loading file...";
'webBrowser1.Navigate ( textBox1.Text );
'richTextBox1.Text = webBrowser1.Document.Body.InnerHtml;
'Status.Text = "File loaded!";
'}
'}

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then

Panel2.Visible = False
Panel3.Visible = False
End If

End Sub

Protected Sub btnImportfromFile_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnImportfromFile.Click
Panel1.Visible = False
Panel3.Visible = False
Panel2.Visible = True
Dim pattern As String = "[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?"
' Dim pattern As String = "as"
Dim reg As New Regex(pattern)
'Dim lst As String = ReadAllText(FileUpload1.PostedFile.FileName.ToString())
sr = File.OpenText(FileUpload1.PostedFile.FileName.ToString())
strng.Append(sr.ReadToEnd())
'Response.Write(strng.ToString())
Dim mc As MatchCollection = reg.Matches(strng.ToString())
Dim list As New ArrayList()
For Each m As Match In mc
list.Add(m.Value)
txtcontactList.Text += m.Value.ToString() + ";"
'Response.Write("hi" + m.Value.ToString() + "
" + strng.ToString())
Next
End Sub

Protected Sub btnMail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMail.Click
Panel1.Visible = False
Panel2.Visible = False
Panel3.Visible = True
txtTO.Text = txtcontactList.Text
'txtTO.Enabled = False
txtFrom.Text = "dharmendra.kumkumar@patni.com"

End Sub

Protected Sub btnSendMail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSendMail.Click


Try
SendMail(txtTO.Text, txtFrom.Text, "zayad.shaikh@patni.com", txtSubject.Text, TextBox4.Text)

Catch ex As Exception
Response.Write("Error Occured" + ex.Message)
End Try
'Response.Write(" ")
' Response.Redirect("Home.aspx?UserID=" + Session.SessionID)
Response.Write("Messase send successfully to " + txtTO.Text + " address")
End Sub
End Class



Attachments

  • File (35429-16420-29080-11655-ImportsContact_aspx_vb.txt)
  • Comments

    Author: ABitSmart16 Dec 2009 Member Level: Gold   Points : 0

    Please add the code as a resource and not as an attachment

    Author: Er. Ram Singh06 Sep 2010 Member Level: Gold   Points : 2

    using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Net.Mail;public partial class Default : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) {//Calling the function SendMail Response.Write( SendMail("meetuchoudhary@gmail.com","meetudmeet@gmail.com","meetudmeet@yahoo.com","Test Mail","Test Mail Body")); } public string SendMail(string toList, string from, string ccList, string subject, string body) { MailMessage message = new MailMessage(); SmtpClient smtpClient = new SmtpClient(); string msg = string.Empty; try { MailAddress fromAddress = new MailAddress(from); message.From = fromAddress; message.To.Add(toList); if (ccList != null && ccList != string.Empty) message.CC.Add(ccList); message.Subject = subject; message.IsBodyHtml = true; message.Body = body; smtpClient.Host = "mail.server.com"; smtpClient.Port = 25; smtpClient.UseDefaultCredentials = true; smtpClient.Credentials = new System.Net.NetworkCredential("info@server.com", "password"); smtpClient.Send(message); msg = "Successful"; } catch (Exception ex) { msg = ex.Message; } return msg; }}



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: