HOW TO: Send email using System.Net.Mail

Rate It (28)

Last post 08-01-2007 8:01 AM by Johnny be good. 173 replies.

Sort Posts:

  • Re: HOW TO: Send email using System.Net.Mail

    04-22-2007, 10:05 PM
    Locked

    Hi,

     I would also like to share my review on two websites that I have mentioned in the blog post http://lavanyadeepak.blogspot.com/2007/03/comprehensive-email-troubleshooting.html


    Besides this, one of the reader wanted to send about thousands of email . I think we can use Persits Mailsender MailQueue. That takes the email and then delivers from its control panel applet.

    Hope this addresses the query. 

    ------------------------------

    Deepak Vasudevan

    http://lavanyadeepak.blogspot.com/

    -------
  • Re: HOW TO: Send email using System.Net.Mail

    05-22-2007, 8:35 PM
    Locked
    • Participant
      1,087 point Participant
    • Ez416
    • Member since 03-31-2006, 7:19 AM
    • Posts 600

    youre code is running. but my question is how can I send a mail if there are attachments?

    ez
  • Re: HOW TO: Send email using System.Net.Mail

    06-11-2007, 8:32 AM
    Locked
    • Member
      23 point Member
    • AjeeetAM
    • Member since 04-26-2007, 5:57 AM
    • Posts 16

    HI

    the code u have written is cool. I liked it.

    ijust want to know how to give my iis default smtp server as my server host. I mean the text that i have to give in the smtpclient.host = <hostname>.....

    and one more thing. CAn i access the server name and port no. from the appsettings section of my webconfig file??

    Thanks and regards

    AjeeethMA

     

  • Re: HOW TO: Send email using System.Net.Mail

    06-11-2007, 1:30 PM
    Locked
    • Member
      35 point Member
    • basher
    • Member since 06-11-2007, 4:55 PM
    • Posts 56

    I'm new to .NET 2 and am having real difficulty with emails getting stuck in my mailroot\queue folder. I'm running Windows 2000 SP4 with IIS5, and have configured a virtual SMTP server, and allowed my "localhost" (127.0.0.1) to connect & relay to this server.

    I have a contact form (in a test website on my PC) created from several TextBox'es. The code-behind file has the following:

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
           
            Dim SendTo As String = "my-email@my-domain.com"
            Dim Subject As String = "Subject ..."
            

    'Name, company, etc come from form on webpage...


            Dim Message As String
            Message = "Message from: " & Name.Text & vbCrLf _
                    & "Company Name: " & Company.Text & vbCrLf _
                    & "Telephone: " & Tel.Text & vbCrLf & vbCrLf _
                    & "Enquiry: " & vbCrLf & Enquiry.Text

            Dim MailObj As New System.Net.Mail.SmtpClient()
            MailObj.Host = "localhost"


            Try
                MailObj.Send(Email.Text, SendTo, Subject, Message)
                Label8.Text = "Thankyou. Your message has been sent."
            Catch ex As Exception
                Label8.Text = ex.Message
            End Try
        End Sub

    My web.config contains:

    <system.net>
            <mailSettings>
                <smtp from="my-email@my-domain.com">
                    <network host="localhost" password="" userName="" />
                </smtp>
            </mailSettings>
        </system.net>
      

    On clicking submit, I get no errors / exceptions, but the email just sits in the queue. SMTP logs show no errors. I've tried changing host to my ISP and using their SMTP server details (with & without authentication) and specifying a smart host too... all to no avail.

    Feel like I'm going round in circles!

    Can anyone assist?
     

     

    Drums are great, but sometimes hitting my PC makes me feel better!
  • Re: HOW TO: Send email using System.Net.Mail

    06-12-2007, 2:25 AM
    Locked
    • Member
      23 point Member
    • AjeeetAM
    • Member since 04-26-2007, 5:57 AM
    • Posts 16

    Your code is good

    But im only getting the Mails to be saved in the queue directory. all the mails are stocked in the InetPub/mailroot/Queue directory. They are not getting cleared from that directory.

    How can i send them from the queue directory to the recepients?

    Do i need to configure iis correctly?

    Or is thats due to invalid From Address???

    Thanks

    AjeeethMA

  • Re: HOW TO: Send email using System.Net.Mail

    06-12-2007, 2:35 AM
    Locked
    • Member
      23 point Member
    • AjeeetAM
    • Member since 04-26-2007, 5:57 AM
    • Posts 16

    Hi

    your code is cool But all the mails are stocked in the InetPub/mailroot/Queue directory. They are not getting cleared from that directory.

    What shall i do

    Thanks

    AjeeethAM

  • Re: HOW TO: Send email using System.Net.Mail

    06-15-2007, 9:30 AM
    Locked
    • Member
      35 point Member
    • basher
    • Member since 06-11-2007, 4:55 PM
    • Posts 56

    I posted on another forum, and got pointed to some help resources...unfortunately, the problem persists, even after following the instructions listed.

    So I started again from the beginning...

    • Re-configured my SMTP server with appropriate relay (localhost) and a smart host (specified in the "smtp.domain-name.com" format as I don't know the IP address for my ISP's servers).
    • I also tried testing the Pickup directory by composing a text file with a destination email address and dropping it in the directory... again, that ends up just sitting in the Queue.
    • SMTP logfile didn't show any error codes.

    I emailed my ISP again. They suggested I try to authenticate using my normal username/password, but where do I enter this in IIS?

    I have found an "outbound security" button within the SMTP server properties "delivery" tab, but the password is sent over the network in plain text.

    Also, it looks like it refers to an authentication for a user on my PC (e.g. Administrator) and not an ISP username/password. I'm very confused.

    Finally, where does web.config come in to play?

    I can see how to add SMTP authentication via ASP.NET Configuration, but it won't help me here because it looks like my problem actually exists within SMTP itself, and has nothing to do with my application.

    Tongue Tied

     

    Drums are great, but sometimes hitting my PC makes me feel better!
  • Re: HOW TO: Send email using System.Net.Mail

    06-18-2007, 12:51 AM
    Locked
    • Member
      10 point Member
    • ksncruz
    • Member since 06-04-2007, 8:39 AM
    • Posts 13

    Hi!

     Why do I get these error? "Namespace or type 'Mail' for the Imports 'System.Net.Mail' cannot be found" Please help. Thanks!

    I'm an old timer in Classic ASP but a newbie on ASP.NET, so please help me out. Thanks in advance
  • Re: HOW TO: Send email using System.Net.Mail

    06-18-2007, 1:49 AM
    Locked

    Hi! I am trying to send in my yahoo and hotmail account with the help of this class file.but it give error. some one please explain the logic behind that when i am trying to send mail to personal web server the it is working.please help

  • Re: HOW TO: Send email using System.Net.Mail

    06-20-2007, 2:04 AM
    Locked
    • Member
      2 point Member
    • Sumit Garg
    • Member since 06-20-2007, 5:33 AM
    • Posts 1

    Hey this article is very good one.Because it also consider the reuseablity of code.So it is a nice one.

    Thanks for gives such types of comments.

  • Re: HOW TO: Send email using System.Net.Mail

    06-20-2007, 12:43 PM
    Locked
    • Member
      166 point Member
    • nufastian
    • Member since 06-20-2007, 4:20 PM
    • Posts 26

    StrongTypes:

    Below is a C# and VB.NET class that demonstrates using System.Net.Mail to send an email.

    Download C# System.Net.Mail Helper
    Download VB.NET System.Net.Mail Helper

    Calling the function from code

    MailHelper.SendMailMessage("fromAddress@yourdomain.com", "toAddress@yourdomain.com", "bccAddress@yourdomain.com", "ccAddress@yourdomain.com", "Sample Subject", "Sample body of text for mail message")

    MailHelper.cs

    using

    System.Net.Mail;

    public class MailHelper
    {
       ///
    <summary>
       
    ///
    Sends an mail message
       
    ///
    </summary>
       
    /// <param name="from">Sender address
    </param>
       
    /// <param name="to">Recepient address
    </param>
       
    /// <param name="bcc">Bcc recepient
    </param>
       
    /// <param name="cc">Cc recepient
    </param>
       
    /// <param name="subject">Subject of mail message
    </param>
       
    /// <param name="body">Body of mail message
    </param>
       
    public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string
    body)
       {
          
    // Instantiate a new instance of MailMessage
          
    MailMessage mMailMessage = new MailMessage();

          // Set the sender address of the mail message
          
    mMailMessage.From = new MailAddress
    (from);
          
    // Set the recepient address of the mail message
          
    mMailMessage.To.Add(new MailAddress
    (to));

     

          // Check if the bcc value is null or an empty string
          
    if ((bcc != null) && (bcc != string
    .Empty))
          {
             
    // Set the Bcc address of the mail message
             
    mMailMessage.Bcc.Add(new MailAddress
    (bcc));
          }

     

          // Check if the cc value is null or an empty value
          
    if ((cc != null) && (cc != string
    .Empty))
          {
             
    // Set the CC address of the mail message
             
    mMailMessage.CC.Add(new MailAddress
    (cc));
          }

     

          // Set the subject of the mail message
          
    mMailMessage.Subject = subject;
          
    // Set the body of the mail message
          
    mMailMessage.Body = body;

     

          // Set the format of the mail message body as HTML
          
    mMailMessage.IsBodyHtml = true
    ;
          
    // Set the priority of the mail message to normal
          
    mMailMessage.Priority = MailPriority.Normal;

          // Instantiate a new instance of SmtpClient
          
    SmtpClient mSmtpClient = new SmtpClient
    ();
          
    // Send the mail message
          
    mSmtpClient.Send(mMailMessage);
       }
    }

    MailHelper.vb

    Imports

    System.Net.Mail

    Public

    Class MailHelper
       ''' <summary>
       '''
    Sends an mail message
       '''
    </summary>
       '''
    <param name="from">Sender address
    </param>
       '''
    <param name="recepient">Recepient address
    </param>
       '''
    <param name="bcc">Bcc recepient
    </param>
       '''
    <param name="cc">Cc recepient
    </param>
       '''
    <param name="subject">Subject of mail message
    </param>
       '''
    <param name="body">Body of mail message</param>
       
    Public Shared Sub SendMailMessage(ByVal from As String, ByVal recepient As String, ByVal bcc As String, ByVal cc As String, ByVal subject As String, ByVal body As String)
          
    ' Instantiate a new instance of MailMessage
          
    Dim mMailMessage As New MailMessage()

          ' Set the sender address of the mail message
          
    mMailMessage.From = New MailAddress(from)
          
    ' Set the recepient address of the mail message
          
    mMailMessage.To.Add(New MailAddress(recepient))

          ' Check if the bcc value is nothing or an empty string
          
    If Not bcc Is Nothing And bcc <> String.Empty Then
             
    ' Set the Bcc address of the mail message
             
    mMailMessage.Bcc.Add(New MailAddress(bcc))
          
    End If

          ' Check if the cc value is nothing or an empty value
          
    If Not cc Is Nothing And cc <> String.Empty Then
          
       ' Set the CC address of the mail message
          
       mMailMessage.CC.Add(New MailAddress(cc))
       
       End If

          ' Set the subject of the mail message
       
       mMailMessage.Subject = subject
          
    ' Set the body of the mail message
          
    mMailMessage.Body = body

          ' Set the format of the mail message body as HTML
          mMailMessage.IsBodyHtml = True
          
    ' Set the priority of the mail message to normal
          
    mMailMessage.Priority = MailPriority.Normal

          ' Instantiate a new instance of SmtpClient
          
    Dim mSmtpClient As New SmtpClient()
       
       ' Send the mail message
          
    mSmtpClient.Send(mMailMessage)
       
    End Sub
    End
    Class

    Web.config

    <?

    xml version="1.0"?>
    <
    configuration>
       
    <
    system.net>
          
    <
    mailSettings>
             
    <
    smtp from="defaultEmail@yourdomain.com">
                
    <
    network host="smtp.yourdomain.com" port="25" userName="yourUserName" password="yourPassword"/>
             
    </
    smtp>
          
    </
    mailSettings>
       
    </
    system.net>
    </
    configuration>
     

     

    Thanks. 

    If this post was helpful, please mark it as an 'Answer'

    MCTS
    http://beenishkhan-technical.blogspot.com
  • Re: HOW TO: Send email using System.Net.Mail

    06-21-2007, 9:36 PM
    Locked
    • Member
      28 point Member
    • mlopilato
    • Member since 06-10-2006, 1:02 AM
    • Posts 11

    StrongTypes,

    I have a problem that has me completely baffled---
    When the controls that I'm referencing in my MailHelper code are in a FormView Control I get the error: 

     

    Compiler Error Message: BC30451: Name 'txtbx_email' is not declared.

    Source Error:

    Line 24:                                         Protected Sub btn_send_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Line 25: 
    Line 26:                                             sender = txtbx_email.Text
    Line 27:                                             name = txtbx_name.Text
    Line 28:                                             phone = txtbx_phone.Text

     

    <asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1" AllowPaging="True">
                                <ItemTemplate>
                                    <table width="325" style="border-right: black 1px solid; border-top: black 1px solid;
                                        border-left: black 1px solid; border-bottom: black 1px solid;">
    
                                        <script runat="server" language="vbscript">
        
                                            Dim Sender, bcc, cc, comment1, comment, name, phone, subject As String
                                            Protected Sub btn_send_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    
                                                sender = txtbx_email.Text
                                                name = txtbx_name.Text
                                                phone = txtbx_phone.Text
                                                comment = txtbx_comment.Text
                                                bcc = ""
                                                cc = ""
                                                subject = hidfld_mls_id.Value
                                                comment1 = "<font face=arial size=3><b>Name: </b>" & name & "<br><b> Phone: </b>" & phone & "<br><b> Comment: </b>" & comment & "</font>"
    
                                                MailHelper.SendMailMessage(sender, "mike@lope.com", bcc, cc, subject, comment1)
                                            End Sub
                                        </script>
    
                                        <tr>
                                            <td colspan="2" align="left">
                                                <asp:Label ID="Label6" runat="server" Text="Request More Information" CssClass="content_text_bold_12"></asp:Label>
                                                <asp:HiddenField runat="server" ID="hidfld_mls_id" Value='<%# Eval("mls_listing_id","I am interested in more information regarding MLS# {0}") %>' />
                                            </td>
                                        </tr>
                                        <tr>
                                            <td colspan="2">
                                                <hr width="325" size="2" color="#03024e" />
                                            </td>
                                        </tr>
                                        <tr>
                                            <td colspan="2" align="left">
                                                <asp:Label ID="Label28" runat="server" Text='<%# Eval("s_mlsname","Contact {0} at:") %>'
                                                    CssClass="content_text_bold_10"></asp:Label>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td colspan="2" align="left">
                                                <asp:Label ID="Label29" runat="server" Text='<%# Eval("mls_office_phone","{0} or Request more information below:") %>'
                                                    CssClass="content_text_bold_10"></asp:Label>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="left" width="50">
                                                <asp:Label ID="Label27" runat="server" Text="*Name:" CssClass="content_text"></asp:Label>
                                            </td>
                                            <td align="left" width="150">
                                                <asp:TextBox ID="txtbx_name" runat="server"></asp:TextBox>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="left" width="50">
                                                <asp:Label ID="Label31" runat="server" Text="*Email:" CssClass="content_text"></asp:Label>
                                            </td>
                                            <td align="left" width="150">
                                                <asp:TextBox ID="txtbx_email" runat="server"></asp:TextBox>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="left" width="50">
                                                <asp:Label ID="Label30" runat="server" Text="Phone #:" CssClass="content_text"></asp:Label>
                                            </td>
                                            <td align="left" width="150">
                                                <asp:TextBox ID="txtbx_phone" runat="server"></asp:TextBox>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="left" colspan="2">
                                                <asp:Label ID="Label32" runat="server" Text="How can we assist you?:" CssClass="content_text"></asp:Label>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="right" colspan="2">
                                                <asp:TextBox ID="txtbx_comment" runat="server" TextMode="MultiLine" Rows="3" Width="300"></asp:TextBox>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="left" width="50">
                                                &nbsp;
                                            </td>
                                            <td align="left" width="150">
                                                <asp:Button ID="Button1" runat="server" Text="Send Info Request" OnClick="btn_send_Click" />
                                            </td>
                                        </tr>
                                    </table>
                                </ItemTemplate>
                            </asp:FormView>

     If I remove the <asp:FormView> from the page the error is gone and the email form works. Help!

    Of course then there isn't any data.

     Thanks,

    Mike

  • Re: HOW TO: Send email using System.Net.Mail

    06-22-2007, 5:03 AM
    Locked
    • Member
      192 point Member
    • KBNSarma
    • Member since 12-23-2005, 9:13 AM
    • Hyderabad, AP, India
    • Posts 67

    Try the following code, which I am using:

    Private Sub SendMailSMS()

    PInfn.SelectCommand = "SELECT Email,Mobile FROM UserID WHERE (UserID = '" & SelOfficer.SelectedItem.Text & "')"

    PInfn.DataBind()

    Dim mSubject, mTo As String

    Dim dv2 As New DetailsView

    dv2.DataSource = PInfn

    dv2.DataBind()

    mSubject =
    "New Call Generated from " & tPName.Text.ToString & ", " & tCity.Text.ToString & " for visit on " & tCallDt.Text.ToString

    mTo = dv2.DataItem("Email")

    mBody.Append(mSubject)

    mBody.Append(vbCrLf)

    mBody.Append(vbCrLf)

    mBody.Append(
    "New Call No. " & tEnqNo.Text.ToString & " Generated by " & Session("SUserID"))

    mBody.Append(vbCrLf)

    mBody.Append(
    "Call for " & HProducts.Value)

    mBody.Append(vbCrLf)

    mBody.Append(
    "Address = " & tPName.Text & "; " & tStreet.Text.Trim & "; " & tLocation.Text.Trim & "; " & tCity.Text.Trim & "; " & tTerritory.Text.Trim)

    mBody.Append(vbCrLf)

    mBody.Append(
    "Call Required on " & tCallDt.Text.ToString & " at " & tTime.Text.ToString)

    mBody.Append(Session("SUserID"))

    Dim MMessage As New MailMessage(Session("SUserEmail"), mTo, mSubject, mBody.ToString)

    Dim emailClient As New SmtpClient(". . . . . . .")  ' Use your smtp or localhost

    If Session("SRemRequest") = "Yes" Then emailClient.Send(MMessage)

    End Sub

    Regards,   KBN Sarma

  • Re: HOW TO: Send email using System.Net.Mail

    07-13-2007, 9:29 AM
    Locked
    • Member
      4 point Member
    • milkdev
    • Member since 07-03-2007, 4:14 PM
    • Posts 2

     
     

  • Re: HOW TO: Send email using System.Net.Mail

    07-18-2007, 4:44 PM
    Locked
    • Contributor
      2,042 point Contributor
    • ojm37
    • Member since 07-30-2003, 7:31 PM
    • AZ
    • Posts 615

    How do I do this NOT from a web-page (i.e., from a Windows program)? I can't seem to be able to tell how to set up the  userName and Password for the smtp server. (Web.config only applies to a web-page....)

    Owen
Page 11 of 12 (174 items) « First ... < Previous 8 9 10 11 12 Next >