Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 

News:



Author Topic: Sending email from .NET issue again!  (Read 20261 times)

onlynaturalfood

Sending email from .NET issue again!
« on: August 04, 2010, 11:51:19 AM »
As many people on the boards noted there seems to be an issue when trying to send email from a website hosted on arvixe through the arvixe mail server. I always get this error:

"The SMTP server requires a secure connection or the client was not authenticated. The server response was: SMTP authentication is required."

I have not tried SSL yet. The arvixe support gives me old asp (obsolete by about 9 years) code.

Here's my C# code (real domains replaced) which works FINE on other providers:


        private bool SendProcessEmail(List<string> toAddresses, string subject, string body,
            string attachmentText, string attachmentName)
        {
            MailMessage message = new MailMessage();
            message.Body = body;
            message.Subject = subject;
            message.From = new MailAddress("info@hostdomain.com");

            SmtpClient mailclient = new SmtpClient();
            mailclient.Host = "mail.hostdomain.com";
            mailclient.Port = 26; //or 25 both don't work anyway
            NetworkCredential aCredential = new NetworkCredential("user@emaildomain.com", "password");

            mailclient.Credentials = aCredential;

            mailclient.UseDefaultCredentials = false;

            foreach (string address in toAddresses)
                message.To.Add(new MailAddress(address));


            mailclient.Send(message);

            return true;
        }

What am i doing wrong?

Arvand

  • Operations Manager
  • *****
  • Posts: 6142
    • View Profile
    • http://www.arvixe.com
Re: Sending email from .NET issue again!
« Reply #1 on: August 04, 2010, 12:07:38 PM »
Quote
As many people on the boards noted there seems to be an issue when trying to send email from a website hosted on arvixe through the arvixe mail server. I always get this error:

"The SMTP server requires a secure connection or the client was not authenticated. The server response was: SMTP authentication is required."

And if you note every post about it, the issue is consequently fixed by themselves fixing their code to authenticate against the SMTP sever.

Also every post does include their code snippets and how they resolved the issue (have you searched for it?)

A code snippet that I have personally tested on our servers has been available at -

https://support.arvixe.com/index.php?_m=knowledgebase&_a=viewarticle&kbarticleid=148

Please let us know if you need further assistance with that. And if you don't wish to use System.web.mail, http://www.systemnetmail.com/'s site has a similar code snippet as well.
Arvand Sabetian
Operations Manager
arvand.sabetian@arvixe.com
1-888-927-8493 [Ext.861]

onlynaturalfood

Re: Sending email from .NET issue again!
« Reply #2 on: August 04, 2010, 02:23:49 PM »
OK. I finally figured it out

The code below in System.Web.Mail works but has been deprecated  in .net 2.0 and after

message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); 
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", (<username@mydomain>);
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", <password>); 

this code below that uses System.Net.Mail compiles fine but IT DOES NOT WORK
NetworkCredential aCredential = new NetworkCredential(<username@mydomain>, <pwd>);
mailclient.Credentials = aCredential;
mailclient.UseDefaultCredentials = false

THe CORRECT CODE that works is below (please not the position of setting the usedefaultcredentials to false).
mailclient.UseDefaultCredentials = false
NetworkCredential aCredential = new NetworkCredential(<username@mydomain>, <pwd>);
mailclient.Credentials = aCredential;

Obviously the call to set credentails to flase must be invalidating all credentials

Arvand

  • Operations Manager
  • *****
  • Posts: 6142
    • View Profile
    • http://www.arvixe.com
Re: Sending email from .NET issue again!
« Reply #3 on: August 04, 2010, 02:28:02 PM »
This outlines the difference between your other environments and ours. Ours does force authentication even when connecting locally and the others your code was on did not require authentication locally.
Arvand Sabetian
Operations Manager
arvand.sabetian@arvixe.com
1-888-927-8493 [Ext.861]

Sarev0k

Re: Sending email from .NET issue again!
« Reply #4 on: August 19, 2010, 09:43:41 AM »
i get this error:
 System.Net.Mail.SmtpException: Server does not support secure connections.

I also get timelimt exceeded depending on the port is use.

Arvand

  • Operations Manager
  • *****
  • Posts: 6142
    • View Profile
    • http://www.arvixe.com
Re: Sending email from .NET issue again!
« Reply #5 on: August 19, 2010, 10:46:15 AM »
If you plan on using SSL then you need to use port 465. I would suggest not using secure as you are connecting locally and there is no risk in connecting without SSL on port 25 on hsot 127.0.0.1
Arvand Sabetian
Operations Manager
arvand.sabetian@arvixe.com
1-888-927-8493 [Ext.861]

Sarev0k

Re: Sending email from .NET issue again!
« Reply #6 on: August 19, 2010, 02:04:13 PM »
If you plan on using SSL then you need to use port 465. I would suggest not using secure as you are connecting locally and there is no risk in connecting without SSL on port 25 on hsot 127.0.0.1

i tryed port 465 too and no success.

I'm only trying to use SSL because when i try to send HTML emails, they are not received, so my guess is that i need ssl enabled..

Arvand

  • Operations Manager
  • *****
  • Posts: 6142
    • View Profile
    • http://www.arvixe.com
Re: Sending email from .NET issue again!
« Reply #7 on: August 19, 2010, 02:04:55 PM »
HTML and SSL are not related.
Arvand Sabetian
Operations Manager
arvand.sabetian@arvixe.com
1-888-927-8493 [Ext.861]

Sarev0k

Re: Sending email from .NET issue again!
« Reply #8 on: August 20, 2010, 02:20:48 AM »
Maybe the receiver email company needs the email to be auth to accept the email.

What i know is, if i send with html = false, email arrives correctly, if i set it = true, it does not arrive.

Code: [Select]
        MailMessage msg = new System.Net.Mail.MailMessage(Globais.email, "receiver@email.com",
            "teste1", "teste1");
        msg.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient();

        System.Net.NetworkCredential mailAuthentication = new
            System.Net.NetworkCredential(Globais.email, Globais.emailPass);
        //here i already used 25, 26, 468, 587
        smtp = new System.Net.Mail.SmtpClient("localhost", 25);

        smtp.EnableSsl = true;

        smtp.UseDefaultCredentials = false;

        smtp.Credentials = mailAuthentication;
        smtp.Send(msg);


Arvand

  • Operations Manager
  • *****
  • Posts: 6142
    • View Profile
    • http://www.arvixe.com
Re: Sending email from .NET issue again!
« Reply #9 on: August 20, 2010, 02:37:33 AM »
You would need access to the logs of the receiver to correctly see why its not going through. How many different receivers have you tried?
Arvand Sabetian
Operations Manager
arvand.sabetian@arvixe.com
1-888-927-8493 [Ext.861]

Sarev0k

Re: Sending email from .NET issue again!
« Reply #10 on: August 20, 2010, 04:41:17 AM »
10 emails from 3 different providers  :(

bpsmicro

  • **
  • Posts: 118
    • View Profile
Re: Sending email from .NET issue again!
« Reply #11 on: August 24, 2010, 06:00:27 AM »
In your code, remove the smtp.EnableSsl=true line. It's wrong (you can't use SSL against Port 25, as was pointed out above), but you don't need or want it anyway since you're connecting locally. I'm pretty amazed any mail is going out with that setting.

.NET 3.5 changes how the MailMessage item works. Your "To" & "From" entries can't be strings, but need to be MailAddress objects or collections.

You want something like this:

Code: [Select]
System.Net.Mail.MailMessage objMM = new System.Net.Mail.MailMessage();
objMM.From = new MailAddress(Globais.email);
objMM.To.Add(new MailAddress("receiver@email.com"));    //Note: this To a collection
objMM.Subject = "My Subject";
objMM.Body = "The Body";
objMM.IsBodyHtml = true;

SmtpClient smtp = new SmtpClient("localhost");
smtp.Credentials - new NetworkCredentials(myUsername, myPassword);
smtp.Send(objMM);

The above code is mostly copied directly from my own code that sends mail from my site, so I know it works. :-)
None of the rest of the stuff in your code is required, and may in fact bugger things up.

Brad.

derekfelton

Re: Sending email from .NET issue again!
« Reply #12 on: September 06, 2010, 08:23:49 PM »
I have used code very similar to bpsmicro's above, and I do indeed get email to send via my Arvixe email account from a web app running in our environment on Arvixe's servers.  The app was apparently able to send the mail, because I received it at the address I specified.

I use "localhost" with no port as my SmtpClient's host.

Two questions related to this technique ...

1.  My recipient address was a Gmail account, and the email came in with a [SPAM] warning on the subject line, e.g., "[SPAM] This is a test email."  In there anything I can do in my mail/SmptClient configuration to prevent that [SPAM] from appearing there?  This code supports a legitimate application with a very small, fixed number of specific and known recipients.

Specifically, can we use a "real" host -- such as mail.(something).arvixe.com -- instead of localhost?  I tried this with the name of our domain's server, mail.bamboo.arvixe.com, but received the error "The SMTP server requires a secure connection or the client was not authenticated. The server response was: SMTP authentication is required."


2.  Short of encrypting name/pwd, decrypting them into variables in code-behind, and then creating the NetworkCredential from those variables, is there any alternate way to create a network credential WITHOUT having your email name and password visible in plain text within the page code?

I'm not afraid to do it the encrypt/decrypt way; I'd just rather determine whether there's an easier, yet secure, way to do it first.

Thank you in advance for any information you can share.
« Last Edit: September 06, 2010, 08:28:40 PM by derekfelton »

Arvand

  • Operations Manager
  • *****
  • Posts: 6142
    • View Profile
    • http://www.arvixe.com
Re: Sending email from .NET issue again!
« Reply #13 on: September 06, 2010, 10:00:27 PM »
Hello,

Quote
1.  My recipient address was a Gmail account, and the email came in with a [SPAM] warning on the subject line, e.g., "[SPAM] This is a test email."  In there anything I can do in my mail/SmptClient configuration to prevent that [SPAM] from appearing there?  This code supports a legitimate application with a very small, fixed number of specific and known recipients.

the [Spam] is not related to the host you use in your code. You should be using localhost as the code resides on the same server as the MTA. Per Gmail's FAQ, the only way for the [SPAM] to be removed is for users to hit "not spam" on your emails. Also, you may wish to check your DNS zone from within the control panel to make sure there is not a SPF record. Sometimes, some users have the wrong SPF record and therefore, the receiving server identifies the emails as spam.

Quote
2.  Short of encrypting name/pwd, decrypting them into variables in code-behind, and then creating the NetworkCredential from those variables, is there any alternate way to create a network credential WITHOUT having your email name and password visible in plain text within the page code?

I'm not sure on that one. You may get a better response on a more specific community forum such as ASP .NET as it is a programming question. Maybe some of our ASP .NET users can comment on this.
Arvand Sabetian
Operations Manager
arvand.sabetian@arvixe.com
1-888-927-8493 [Ext.861]

bpsmicro

  • **
  • Posts: 118
    • View Profile
Re: Sending email from .NET issue again!
« Reply #14 on: September 07, 2010, 05:57:10 AM »
2.  Short of encrypting name/pwd, decrypting them into variables in code-behind, and then creating the NetworkCredential from those variables, is there any alternate way to create a network credential WITHOUT having your email name and password visible in plain text within the page code?
I'd do the encrypt/decrypt thing. You can store the encrypted blogs in your web.config, which IIS is not supposed to allow access to from the outside world (without credentials to your site content overall). The irritant is that you'll need to store the encryption key somewhere too, so in reality if somebody *was* able to see into your web.config to get the blobs, they'd almost certainly also be able to get the key, so you're no further ahead.

Personally I'd rather if the SMTP connection *to* localhost *from* localhost didn't require credentials, since the mail server itself knows it's a "local" (and therefore as secure as required) connection.

And as Arvand said, that would have no effect about whether or not your messages are tagged as Spam. How the receiving servers decide a message is spam is a bit of black magic, and in many cases not "documented" (since, if it was, teh spammers would have the info required to bypass). Normally, though, the message headers will include one or more x-headers that'll give some clues. It may be a bad SPF record, content-related, or something else entirely.

Brad.

 

Web Hosting

Linux Hosting
ASP .NET Hosting
Business Hosting
Reseller Hosting
Dedicated Servers

Server Alerts
Acceptable Use Policy
Terms of Service
Privacy Policy

Affiliate Program
Site Map

Blog  Hosting

WordPress Hosting
ExpressionEngine Hosting
Movable Type Hosting
Habari Hosting
Textpattern Hosting


CMS  Hosting

Joomla Hosting
Drupal Hosting
MODX Hosting
Concrete5 Hosting
Xoops Hosting
Ecommerce  Hosting

nopCommerce Hosting
OpenCart Hosting
Magento Hosting
osCommerce Hosting
Tomato Cart Hosting


Video Sharing Software  Hosting

ClipBucket Hosting
PHPMotion Hosting
ClipShare Hosting
osTube Hosting
vShare Hosting
Forum  Hosting

XenForo Hosting
vBulletin Hosting
MyBB Hosting
phpBB Hosting
Simple Machines Hosting


Social Network and Community Website  Hosting

SocialEngine Hosting
Elgg Hosting
Dolphin Hosting
phpfox Hosting
OxWall Hosting
Copyright © 2003-2014 Arvixe, LLC. All trademarks are property of their legal owner.