ASP Email sending error - asp.net

I have found this site very useful for all my previously faced problems, However i couldnt get help with the following.
I have developed a website which is able to send emails. On localhost this works absolutely fine. when i say localhost, i am able to recive the emails sent, but when i upload onto server i face this error when it starts the process of sending emails.
"A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond IPADDRESS:PORT"
Tried ping on the adrress for the port and ping is working.
Here is the code
string strFrm = ConfigurationManager.AppSettings["FromAddress3"].ToString();
string[] receive = {"emailaddress1","emailaddress2","emailaddress3","emailaddress4"};
string subject = "New registration";
string body = "<html><head><title>Registered Candidates</title></head><body>bla bla bla</body></html>";
//I however have put reg exp validator on the form
if(txtEmail.Text.Contains("#") && txtEmail.Text.Contains("."))
{
for (int i = 0; i <= receive.Length - 1; i++)
{
MailMessage msg = new MailMessage(strFrm, receive[i], subject, body);
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.Send(msg);
}
Response.Redirect("Thankyou.html");
Web.config
<mailSettings>
<smtp from="from address">
<network host="server" port="25"
userName="username" password="password" />
</smtp>
</mailSettings>
Please help. I upload onto my server via precompilation of the site and upload the files.

Make sure you're pointing to an SMTP service on your production server, it may not work on "localhost" as it does on your development machine. And pinging the server doesn't really tell you if it has SMTP enabled.
I generally prefer setting up SMTP for my sites in web.config:
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="you#yourdomain.com">
<network host="localhost" port="25" userName="user" password="pass" />
</smtp>
</mailSettings>
</system.net>
UPDATE:
If your code is working on your development machine, and it fails on the server with the same configuration, then there's probably something blocking. I would suggest trying to play around with a simple implementation that does nothing but test the servers SMTP configuration. You may want to try the <smtp deliveryMethod="SpecifiedPickupDirectory">, it's quite helpful when testing code that sends out emails. See the SmtpDeliveryMethod Enumeration on MSDN.

Solved.. :D
My hosting server was godaddy and my hosting plan supported only age old system.web.mail i.e CDOSYS concept.
Here is the code.
using System.Web.Mail;
private void SendEmail()
{
const string SERVER = "relay-hosting.secureserver.net";
MailMessage oMail = new System.Web.Mail.MailMessage();
oMail.From = "emailaddress#domainname";
oMail.To = "emailaddress#domainname";
oMail.Subject = "Test email subject";
oMail.BodyFormat = MailFormat.Html; // enumeration
oMail.Priority = MailPriority.High; // enumeration
oMail.Body = "Sent at: " + DateTime.Now;
SmtpMail.SmtpServer = SERVER;
SmtpMail.Send(oMail);
oMail = null; // free up resources
}
Thanks Jakob for participating actively! :)

Related

How do i set up asp.net web forms app to send smtp email on windows 7 so i can test?

I am trying to test send email thru smtp in asp.net web forms app on windows 7. I get timeout. I am using my fastmail account to test with. I am pretty confused at this point as i am not sure if i also need to set up smtp email thru my iis console manager. Here is the settings i have in my console manager first. I am confused to how this process works. Does asp.net app first send to smtp email then send it to fastmail smtp?
smtp email page
e-mail address: "is set to my fastmail address"
deliver email to smtp server is checked.
smtp server: mail.messagingengine.com
use localhost is unchecked
port: 25 fastmail port is 465 but was told to set this to 25
authentication settings: i have this set to my email address and password
here is the session state page
session state mode settings: in process
cookie settings
mode: use cookies
name: asp.net_sessionid
use hosting identity for impoersonation is checked
next here is my web.config file in the asp.net app
<system.net>
<mailsettings>
<smtp deliveryMethod="Network" from"roger <myname#fastmail.us>">
<network host="mail.messagingengine.com" userName="myname#fastmail.us" password="myemailpassword" port="465" enableSsl="true" />
</smtp>
</mailSettings>
</system.net>
next here is my c# behind code
protected void btn_send_Click(object sender, EventArgs e)
{
try {
string sendername = txtbx_name.Text.ToString();
MailMessage mymessage = new MailMessage();
mymessage.Subject = txtbx_subject.Text.ToString();
mymessage.Body = txtbx_message.Text.ToString();
mymessage.From = new MailAddress("george#georgebush.me", sendername);
mymessage.To.Add(new MailAddress("myemail address", "Roger"));
SmtpClient mysmtpclient = new SmtpClient();
mysmtpclient.UseDefaultCredentials = false;
mysmtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;
mysmtpclient.Send(mymessage);
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + myStringVariable + "');", true);
}
catch (Exception ex)
{
string message = ex.ToString();
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + message + "');", true);
}
}
am assuming the html and css isnt needed here. Any help is appreciated. I cant even get an error to catch in try catch statement. I am pretty lost at this point as i am new to asp.net.
I guess your code is correct, you must add address for:
mymessage.To.Add(new MailAddress("myemail address", "Roger"));
Instead of "mymail address" just put an email address
Did you check your setting file, you forgot an equal mark after from as below:
<smtp deliveryMethod="Network" from"roger <myname#fastmail.us>">

Sending email through windows hosting?

How to create a email sending simple web service in Windows hosting in a file like mailer.asp as following mailer.php does in php environment. Gets 3 variables and sends email:
<?php
mail($_GET['email'], $_GET['subject'], $_GET['message']);
?>
I came up with following but it is giving 500 internal error:
<%
Set myMail=CreateObject("CDO.Message")
myMail.Subject=Request.QueryString("subject")
myMail.From="mail#example.com"
myMail.To=Request.QueryString("email")
myMail.TextBody=Request.QueryString("message")
myMail.Send
set myMail=nothing
%>
Requirement is it should work in Windows environment, no matter it is VBscript, C#, ASP, or ASPX etc
You could do something like this
config file to include the following
<system.net>
<mailSettings>
<smtp from="your email address" deliveryMethod="Network">
<network defaultCredentials="false" host="your host" password="your password" userName="your username"/>
</smtp>
</mailSettings>
and then the C# code can look something like
var smtpClient = new SmtpClient();
var msg = new MailMessage();
msg.To.Add("to email address");
msg.Subject = "Some subject";
msg.Body = "Some message";
smtpClient.Send(msg);
This is something really simple you can expand from this as you wish

ASP.NET Send Email

I am trying to send a email when user clicks submit in a contact us page but for some reason its not working, what am I doing wrong? (PS email & password was omited from this code snippet but included in actual solution.
Thanks
Code in web.config:
<system.net>
<mailSettings>
<smtp from="order#test.com">
<network host="smtp.gmail.com"
userName="" //my email
password="" //password deleted for privacy reasons
defaultCredentials="false"
port="456"
enableSsl="true" />
</smtp>
</mailSettings>
code in asp.net contact form:
protected void btnSubmit_Click(object sender, EventArgs e)
{
MailMessage mail = new MailMessage();
mail.To.Add(new MailAddress("test#gmail.com"));
mail.Subject = "Test";
mail.Body = "Message was sent from" + txtName.text + txtComment.text;
SmtpClient smtp = new SmtpClient();
smtp.SendAsync(mail, null);
}
Gmails uses port 465, not 456 for SSL SMTP. From here:
Outgoing Mail (SMTP) Server - requires TLS1 or SSL: smtp.gmail.com
Use Authentication: Yes
Port for TLS/STARTTLS: 587
Port for SSL: 465
The "other" possible reason:
Does your code "work" when developing locally, but stops working when you "publish"/ftp/copy, etc. your files to your web host?
If Yes: check your host's trust settings for ASP.Net. If it's medium trust (which is likely in shared hosting), then note that you cannot use any port other than port 25 for SMTP in medium trust.
It works locally (dev) because in local dev/VS environment, ASP.Net runs in full trust.
REF (MSDN Blogs): SMTP Problems when ASP.Net is not running in full-trust

my send mail not found in inbox

i want to send mail in asp.net form here is my coding
protected void Button1_Click(object sender, EventArgs e)
{
SmtpClient smtp = new SmtpClient("192.168.1.2",Convert.ToInt32 (25));
System.Net.NetworkCredential cre = new System.Net.NetworkCredential();
smtp.Credentials = cre;
MailMessage message = new MailMessage();
message.To.Add(new MailAddress("uamrit#gmail.com"));
message.IsBodyHtml = true;
message.Body = "<html><head><body><p> this is Demo for sending mail. </p></body></head></html>";
message.Subject=("response from the web sitre");
message.From = new MailAddress("uamrit#gmail.com");
try
{
smtp.EnableSsl = false;
smtp.UseDefaultCredentials = true;
smtp.Send(message);
Response.Write("Your Email has been sent sucessfully -");
}
catch (Exception exc)
{
Response.Write("Send failure: " + exc.ToString());
}
}
in web.config
<system.net>
<mailSettings>
<smtp from="uamrit#gmail.com">
<network host="192.168.1.299" port="25" userName="uamrit" password="*****"/>
</smtp>
</mailSettings>
</system.net>
this show mail send successfully but when we check my gmail account there no one mail for me why this happen.
what the procedure to send mail
plz send me full coding
There can be multiple reasons.Did you check your SPAM inbox? Also I see that you are pretending to send an email from GMAIL account through another server and I am sure GMAIL will not like it and migh blacklist your email address.
If you are planning to use GMAIL then why not use GMAIL SMTP settings?
In your config, you define a user name and password - yet, in your code, you specify
smtp.UseDefaultCredentials = true;
You need to decide which one to use - default credentials, or the username/password in the config?
I would think if you define a username/password in your config, you don't want to override that in your code by specifying UseDefaultCredentials = true...
If that doesn't solve you problems, I would try to specify the SMTP mail to go to a directory of your choosing, and see if the mails (stored as *.eml files in that directory) are really created at all. To do so, use this config:
<system.net>
<mailSettings>
<smtp from="uamrit#gmail.com" deliveryMethod="specifiedPickupDirectory">
<specifiedPickupDirectory>C:\temp\mails</specifiedPickupDirectory>
</smtp>
</mailSettings>
</system.net>
You need to make sure the directory defined does already exist, though!

Cannot get IIS pickup directory

I’ve been using the Smtp server 127.0.0.1 .The error I get:
System.Net.Mail.SmtpException: Cannot get IIS pickup directory.at System.Net.Mail.IisPickupDirectory.GetPickupDirectory().
This Error occured ,when Email send from ASP web page.But EMail send from ASP.NET page,error is not occurred. Plz help .
Unfortunately, this exception is raised when any kind of problem occurs while trying to determine the location of IIS/SMTP pickup directory. A common cause is missing IIS SMTP service.
If you are sending mail using System.Net.Mail.SmtpClient, try setting the pickup directory manually:
// C#
var client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
client.PickupDirectoryLocation = ...;
client.Send(...);
Or set this in ASP.NET's Web.config instead:
<configuration>
<system.net>
<mailSettings>
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory
pickupDirectoryLocation="..." />
<network defaultCredentials="false" />
</smtp>
</mailSettings>
</system.net>
</configuration>
Alternatively, use SmtpDeliveryMethod.Network method instead and sent the Host and Port properties to your SMTP server.
More information: http://forums.iis.net/p/1149338/1869548.aspx
The pickup directory is stored in the II6 Metabase, so if the account that your web-app runs as does not have access to the required nodes, this error can be thrown (had this myself). Metabase permissions are seperate from file permissions, so you explore it with Metabase explorer:
http://www.microsoft.com/downloads/details.aspx?FamilyID=56fc92ee-a71a-4c73-b628-ade629c89499&displaylang=en (its part of the IIS resource kit)
These nodes need to have read permission given to your web-app user:
\LM\SmtpSvc
\LM\SmtpSvc\1
I was having this same error on Windows 7 with code that worked fine on XP. After much trial and error. I setup IIS to store mail in a pickup directory. But I still had the error.
In my code I commented out the line:
client.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
Removing this line of code worked, not sure why. Hope it works for you too because this issue is a real time waster to troublshoot.
I did NOT have to change any permissions on the directory.
I did NOT have to modify the metabase.
I did NOT have to modify the web.config (which I really didn't want to do because I only want the emails placed into a directory while I'm doing development on my local machine, not in production - I didn't want two different web.config files to maintain).
You can also specify it for your unittest project:
public enum TestContextKeys { EmailPickupDirectory, ... };
[TestClass]
public class AssemblyInitializer
{
[AssemblyInitialize]
public static void Init(TestContext testContext)
{
string configPath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
XDocument xmlConfig = XDocument.Load(configPath);
var emailPickupDirectory = xmlConfig.Element("configuration")
.Element("system.net")
.Element("mailSettings")
.Element("smtp")
.Element("specifiedPickupDirectory")
.Attribute("pickupDirectoryLocation")
.Value;
testContext.Properties[TestContextKeys.EmailPickupDirectory.ToString()] = emailPickupDirectory;
}
And your test code:
[TestMethod]
public void TestEmailRegistration()
{
MyApp app = new MyApp();
app.RegisterUser("Johny Cash",...);
string emailPickupDirectory = (string)_testContext.Properties[TestContextKeys.EmailPickupDirectory.ToString()];
string[] allEmails = Directory.GetFiles(emailPickupDirectory);
string[] recentEmails = allEmails.Where(e => new FileInfo(e).CreationTime.AddMinutes(1) > DateTime.Now).ToArray();
//check that the registration email was sent
foreach (var email in recentEmails)
{
string content = File.ReadAllText(email);
if (content.Contains("Johny Cash") && content.Contains("successful") && content.Contains("registration"))
{
File.Delete(email);
return;//OK found
}
}
Assert.Fail("Registratoin email has not been sent to Johny Cash");
}
[TestMethod]
public void TestEmailPickupDirectoryConfiguration()
{
string emailPickupDirectory = (string)_testContext.Properties[TestContextKeys.EmailPickupDirectory.ToString()];
MailAddress mailFrom = new MailAddress("testemailpickupdirectory#example.com", "Tester");
MailAddress mailTo = new MailAddress("testemailpickupdirectory#testing.com", "Tester2");
string subject = "Test Message TestEmailPickupDirectory";
using (SmtpClient sc = new SmtpClient())
{
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add(mailTo);
mail.Subject = subject;
mail.From = mailFrom;
mail.IsBodyHtml = true;
mail.BodyEncoding = System.Text.Encoding.GetEncoding("ISO-8859-9");
mail.Body = "<html><body>";
mail.Body += "TestEmailPickupDirectory";
mail.Body += "</body></html>";
sc.Send(mail);
}
string[] allEmails = Directory.GetFiles(emailPickupDirectory);
string[] recentEmails = allEmails.Where(e => new FileInfo(e).CreationTime.AddMinutes(1) > DateTime.Now).ToArray();
foreach (var email in recentEmails)
{
string content = File.ReadAllText(email);
if (content.Contains(mailFrom.Address) && content.Contains(mailTo.Address) && content.Contains(subject))
{
File.Delete(email);
return;//OK found
}
}
Assert.Fail("EmailPickupDirectory configuration may be wrong.");
}
Create the app.config file in your unittest project if not exists or merge these lines with existing app.config.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.net>
<mailSettings>
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="d:\temp\Emails\" />
</smtp>
</mailSettings>
</system.net>
</configuration>

Resources