System.web.mail gmail error: System.Web.HttpException: The transport failed to connect to the server

Hello there,

Im using VWD2008 and sql server express for the development of my school project..I have an application which could send newsletter to subscribed user, but i get this error upon sending the mail:

A general exception occurred! System.Web.HttpException: The transport failed to connect to the server. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Runtime.InteropServices.COMException (0x80040213): The transport failed to connect to the server. --- End of inner exception stack trace --- at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters) at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams) at System.Web.Mail.SmtpMail.LateBoundAccessHelper.CallMethod(Object obj, String methodName, Object[] args) --- End of inner exception stack trace --- at System.Web.Mail.SmtpMail.LateBoundAccessHelper.CallMethod(Object obj, String methodName, Object[] args) at System.Web.Mail.SmtpMail.CdoSysHelper.Send(MailMessage message) at System.Web.Mail.SmtpMail.Send(MailMessage message) at techtalkdemos.MassMailer.Send_Mail(Object sender, EventArgs e) in c:\Documents and Settings\Wah\Desktop\test\InternalMail.cs:line 65

this is my codebehind file:

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Mail;
using System.Web.Configuration;
using System.Configuration;
using System.Collections.Specialized;

namespace techtalkdemos
{
    public class MassMailer : Page
    {
        
        // declare the server controls to be used on the WebForm
        protected Panel pnlForm;
        protected Label lblConfirmation;
        protected TextBox txtFrom;
        protected TextBox txtSubject;
        protected TextBox txtBody;
        protected Button btnSubmit;
        
        public void Send_Mail(object sender, EventArgs e)
        {
            pnlForm.Visible = false;
            
            // replace the database connection information here with your own login information


            SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString);

            SqlCommand comm = new SqlCommand("SELECT DISTINCT Email FROM BreakingNews",conn);
            SqlDataReader dr;

            System.Web.Mail.MailMessage mailer = new System.Web.Mail.MailMessage();

            mailer.From = txtFrom.Text.Trim();
            mailer.Subject = txtSubject.Text.Trim();
            mailer.Priority = MailPriority.High;
            mailer.Body = txtBody.Text.Trim();
            mailer.BodyFormat = MailFormat.Html;
            mailer.Bcc = "xxx@gmail.com";
            
            
            try
            {
                conn.Open();
                dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
                
                    // loop through the records in the database table and send a message to each of the recipients
                    while(dr.Read())
                    {
                        mailer.To = dr[0].ToString();
      

                        System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com";
                        mailer.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
                        mailer.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "xxx@gmail.com");
                        mailer.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "xxx");
                        mailer.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "587");
                        mailer.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
                       
                        System.Web.Mail.SmtpMail.Send(mailer);


                    
                    }
                    
                dr.Close();
                
                lblConfirmation.Text = "Your message was successfully sent to its recipients!";
            }
            catch(SqlException ex)
            {
                Response.Write("A database-related exception occurred!
" + ex.ToString()); } catch(Exception ex) { Response.Write("A general exception occurred!
" + ex.ToString()); } finally { if(conn.State == ConnectionState.Open) { conn.Close(); } } } } }

 

i want to stick to system.web for now cause im not really sure how to change this to system.net..Is there any wrong with my settings above?

0
singapork
7/31/2008 2:26:40 PM
asp.net.getting-started 91979 articles. 4 followers. Follow

6 Replies
1230 Views

Similar Articles

[PageSpeed] 8

Rather than using System.Web.Mail use "System.Net.Mail" as a namespace.

It will help you.


-Sugan

Remember to click “Mark as Answer” on the post If you get answer from this post(s) !
0
sugan
7/31/2008 3:51:20 PM

System.Web.Mail has been depreciated. Please use System.Net.Net, below is how you'd use it to send an email via Gmail.

using System.Net;
using System.Net.Mail;

NetworkCredential loginInfo = new NetworkCredential("yourUsername@gmail.com", "yourGmailPassword");
MailMessage msg = new MailMessage();

msg.From = new MailAddress("yourUsername@gmail.com");
msg.To.Add(receipents.ToString());
msg.Subject = "Test Message";
msg.Body = "This is a simple test email";
msg.IsBodyHtml = true;

try
{
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = loginInfo;
client.Send(msg);
}
catch (SmtpException ex)
{
ErrorLabel.Text = "The following error occurred: " + "&lt;br /><br />" + ex.Message;
return;
}
ErrorLabel is just a label for displaying exception messages 
  
My Blog
http://Lspence.blogspot.com

(Please MARK this post as ANSWERED, if you find it helpful)
0
lspence
7/31/2008 4:19:01 PM

Hello There, thanks for arll your suggestions..i changed the code to the following but i got this error now 

 Cannot assign to "ADD" because its a method group...

 

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Configuration;
using System.Collections.Specialized;
using System.Net;
using System.Net.Mail;

namespace techtalkdemos
{
    public class MassMailer : Page
    {
        
        // declare the server controls to be used on the WebForm
        protected Panel pnlForm;
        protected Label lblConfirmation;
        protected TextBox txtFrom;
        protected TextBox txtSubject;
        protected TextBox txtBody;
        protected Button btnSubmit;
        
        public void Send_Mail(object sender, EventArgs e)
        {
            pnlForm.Visible = false;
            
            // replace the database connection information here with your own login information


            SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString);

            SqlCommand comm = new SqlCommand("SELECT DISTINCT Email FROM BreakingNews",conn);
            SqlDataReader dr;

            NetworkCredential loginInfo = new NetworkCredential("xxx@gmail.com", "xxx");
            MailMessage mailer = new MailMessage();
            mailer.From = new MailAddress(txtFrom.Text.Trim());
            mailer.Subject = txtSubject.Text.Trim();
            mailer.Body = txtBody.Text.Trim();
           // mailer.BodyFormat = MailFormat.Html;

            
            
            try
            {
                conn.Open();
                dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
                
                    // loop through the records in the database table and send a message to each of the recipients
                    while(dr.Read())
                    {
                        
                        mailer.To.Add = dr[0].ToString();
                        SmtpClient client = new SmtpClient("smtp.gmail.com", 587); 
                        client.EnableSsl = true; 
                        client.UseDefaultCredentials = false; 
                        client.Credentials = loginInfo;
                        client.Send(mailer);


                    
                    }
                    
                dr.Close();
                
                lblConfirmation.Text = "Your message was successfully sent to its recipients!";
            }
            catch(SqlException ex)
            {
                Response.Write("A database-related exception occurred!<br>" + ex.ToString());
            }
            catch(Exception ex)
            {
                Response.Write("A general exception occurred!<br>" + ex.ToString());
            }
           
            finally
            {
                if(conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
        }            
    }
}
        

 

however i got this error now : Cannot assign to "ADD" because its a method group...

0
singapork
8/1/2008 3:02:41 AM

singapork:
however i got this error now : Cannot assign to "ADD" because its a method group...
 

Just as the error says Add is a method group. Try changing the following line:

singapork:
mailer.To.Add = dr[0].ToString();
 

To this: mailer.To.Add(dr[0].ToString());


My Blog
http://Lspence.blogspot.com

(Please MARK this post as ANSWERED, if you find it helpful)
0
lspence
8/1/2008 3:12:25 AM

 

Thank you so much! i got it working now!  :)
0
singapork
8/1/2008 3:31:34 AM

Hay nice solution...

can some one help me with this instaed of getting my emails from the database,...

i would like to enter my email  in a textbox separated with comma.

eg.  info@jams.com, sales@jams.com

 

 

James

 

 

 

0
scriptures
9/3/2008 11:48:10 AM
Reply:

Similar Artilces:

System.Exception: System.Web.HttpException: The transport failed to connect to the server. System.Runtime.InteropServices.COMException (0x80040213): The transport failed to connect to the server.
I have an application which creates about 50 XML packages and sends a notification about the success/failure of every package. I am running this code in threads and I have restricted maximum no. of threads that can be active at any time to 2.So the package generation and the notification would be performed for 2 packages in parallal. But while sending notifications to users, I am getting the below error only for very few packages[ 1 or 2 out of 50] . The same base code is used by all the packages but i am getting this error intermittently. System.Exception: Error sending mail to [So...

System.Web.Mail "The transport failed to connect to the server"
I have been trying to get this email form working all night.  I just moved my site to a new server and now my forms do not work.  <% @Import Namespace="System.Web.Mail" %><script language="vb" runat="server"> Sub btnSendFeedback_Click(sender as Object, e as EventArgs)   'Create an instance of the MailMessage class  Dim mail as New MailMessage()   'Set the properties - send the email to the person who filled out the  'feedback form.  mail.To = "info@mydomain.com"  mail.From = txtEmail.Text   'If you want to CC this email to someon...

Help for Visual web dev isolate system.web.mail to system.net.mail
Hi all,  I'm new user to visual web developer express software. Laterly i found this application isolate quite some of the component namespace, like system.web.mail to net.mail. I not well understand how system.net.mail functional?? Does it act same as system.web.mail?? My coding below: change system.web.mail to system.net.mail which causing a lot error... *****Can anybody correct me or advise me how to make this code work in system.net.mail?????? <%@import Namespace="System.net.Mail" %> <script runat="server">    Public FromAddress As String = "" .......

What additional advantage we get through System.net.mail over the old system.web.mail?
What additional advantage we get through System.net.mail over the old system.web.mail?Is it needed to configure the smtp server? Plz tell me the steps to develop the email sending facility in our applicationSwati Jain Take a look at www.systemnetmail.com.JeffPlease: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue....

System.web.mail to System.net.mail
I converted a asp.net web app from VS 2003 to VS 2005. i now get error message when i build it  stating system.web.mail  is obsolute,  use system.net.mail i redefine to system.net.mail but now i get other error message such as smtpMail does not exist in the current context. cannot implicitly convert type string to System.Net.Mail.MailAddress   so System.Web.Mail want work in ASP.net web app convert to VS 2005 running on framework 2.0?           because if i change to System.Net.Mail  i am ...

Working with System.Web.Mail but not with System.Net.Mail
Hi, i am having trouble sending e-mails using System.Net.Mail namespace while there is no problem with System.Web.Mail. I am  using remote smtp server with no authentication.  My code as shown below:1 try2 {3 MailMessage message = new MailMessage();4 message.BodyFormat = MailFormat.Html;5 message.To = "admin@mydomain.com";6 message.From = "test@mydomain.com";7 message.Subject = "Test Smtp";8 message.Body = "TestMail 123";9 10 SmtpMail.SmtpServer = "mail.mysmt...

Changing System.Web.Mail to System.Net.Mail
I am trying to learn ASPI have been using the Visual Web Developer 2005I have the following line that I can not work out how to convert.<%Dim FromForm As String = "Mail@Myname.co.uk"Dim ToMe As String = "Me@myname.co.uk"Dim FormSubject As String = "Contact Infomation"Dim EmailText As String = "Blar Blar Blar"System.Web.Mail.SmtpMail.Send(FromForm,ToMe,FormSubject,EmailText)%>It tells me that I cannot use System.Web.Mail any more and should use System.Net.Mail. I have been unable to work out what the new code should be to get it to work.The Language is VBCan anyone tell me how to change...

switching from system.web.mail to system.net.mail
I have been converting my code from system.web.mail to system.net.mail.  I have been able to convert the to, from, cc, and message body to the new format easily but...my question is how do i convert this into system.net.mail format?SmtpMail.SmtpServer = "server";                SmtpMail.Send(mail);Would this be the solution?SmtpClient client = new SmtpClient("server");                client.Send(mail); Here's a full example of how to per...

System.Web.Mail vs System.Net.Mail
Hi AllI was using System.Web.Mail in an application on our test server.  That worked fine.  I never had to specify a server or change any settings.  It just works.  Whatever settings the server has set up are used.  This is great.I saw that System.Web.Mail has been deprecated, so I'm trying to use System.Net.Mail.  I'm having a problem.  Net.Mail doesn't use whatever settings Web.Mail used.  I can't send e-mail with Net.Mail.  I see several people use the web.config file to set the server up, but I can't get that to work. I jus...

Migrating from System.web.mail to System.net.mail
Hi Every1,I've recently used the System.web.mail to send mail, using the IIS's smtp server.Doing this, i didn't really need to do anything in the web.config file or give any special parameters to the client. It just assumed i was working with my very own smtp service that came with the IIS and everything worked fine.Now i migrated to System.net.mail and now i need to insert my smtp service's host location under <system.net> in the web.config file.However, i don't know what to write there so the client will use my smtp sevice (host="localhost" doesn't work)can any1 help me on this one?t...

difference between System.Web.Mail and System.Net.Mail
What is difference between System.Web.Mail and System.Net.Mail  namespace?What is use of both ? System.Web.Mail was in .NET Framework v1.1System.Net.Mail is in .NET Framework v2.0 and greaterSame use, although there is more functionality in System.Net.Mail since it is a later version of the framework. Darrell Norton, MVPDarrell Norton's BlogPlease mark this post as answered if it helped you!  What All-Star said. I assume the reason it was moved to a different namespace is because the internet is actually a suite of technologies: e-mail, ftp, http (web), etc. E-mail and...

Error When Debugging (Unable to start debugging on the web server. Unable to connect to the web server. Varify that the web server is running and that incoming HTTP request are not blocked by a firewa
Hi, When im going to run asp.net website its giving this error. Unable to start debugging on the web server. Unable to connect to the web server. Varify that the web server is running and that incoming HTTP request are not blocked by a firewall.  please help me to solve this problem. Thank you. Pubudu  If you are running your site off of a different machine than you are developing on, you need to install and configure the Visual Studio Remote Debugger on that machine. try to change the address from http://localhost to http://yourIP...   i hope it's worked...mak...

System.Web.HttpException: Unable to validate data. at System.Web.Configuration.MachineKeySection.GetDecodedData Error
 I get the following very intermittant error. System.Web.HttpException: Unable to validate data. at System.Web.Configuration.MachineKeySection.GetDecodedData The form encoding type is for uploading files (the multipart one). Is this a similar error the ViewState validation "bug" where the page throws an error if you naviagate too quickly and don't give the page enough time to load in the encrypted viewstate?  Regards, Chris. http://www.theserverside.net/discussions/thread.tss?thread_id=40638...

Error Message: Method not found: 'Void System.Web.UI.ScriptManager.RegisterClientScriptBlock(System.Web.UI.Page, System.Type, System.String, System.String, Boolean)'.
  I have a website which is running on IIS 5.1. Wen ever I am trying to access any page based on .net framework 2.0 I get this error Error Message: Method not found: 'Void System.Web.UI.ScriptManager.RegisterClientScriptBlock(System.Web.UI.Page, ...