Sending email through windows hosting? - asp.net

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

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 With System.Net.Mail

I am trying to send a mail from admin#bitcoindk.dk to hejmeddig#gmail.com by doing this:
MailMessage mailObj = new MailMessage();
mailObj.From = new MailAddress("admin#bit.dk");
mailObj.To.Add("hejmeddig#gmail.com");
mailObj.Body = "HEJ";
mailObj.Subject = "HEJ";
SmtpClient SMTPServer = new SmtpClient();
SMTPServer.Send(mailObj);
In my web.config, i have this:
<system.net>
<mailSettings>
<smtp from="admin#bitcoindk.dk">
<network host="mail.bitcoindk.dk" port="25" userName="admin#bitcoindk.dk" password="password" />
</smtp>
</mailSettings>
</system.net>
When i send the mail, i get this exception
Transaction failed. The server response was: 5.7.1 <hejmeddig#gmail.com>: Relay access denied
If i send a mail to admin#bit.dk, it works fine. But if i send to hejmeddig#gmail.com, or any other mail, i get the exception. I am using Uno Euro's mail service.
This is what I use to send emails. See if something like this might help solve your problem.
SmtpSection cfg = NetSectionGroup.GetSectionGroup(WebConfigurationManager.OpenWebConfiguration("~/web.config")).MailSettings.Smtp;
MailMessage objMessage = new MailMessage();
objMessage.IsBodyHtml = true;
objMessage.From = new MailAddress(cfg.From);
objMessage.Subject = "Some Subject";
objMessage.Body = sb.ToString();
objMessage.To.Add(new MailAddress("google#gmail.com"));
SmtpClient client = new SmtpClient(cfg.Network.Host);
client.Send(objMessage);
Web.config
<mailSettings>
<smtp from="fromemail#mydomain.com">
<network host="mail.mydomain.com" port="25" userName="mydomain.com" password="myPassword" />
</smtp>
</mailSettings>
I was using the wrong outgoing mail server.

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

ASP Email sending error

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! :)

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!

Resources