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.
Related
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
Below is my code for sending mail...
public void SendBy(string to, string subject, string body)
{
MailMessage nM = new System.Net.Mail.MailMessage();
nM.To.Add("abc#compulynx.org");
nM.Subject = subject;
nM.Attachments.Add(new Attachment(oStream, Fname));
nM.Body = body;
System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
System.Net.Configuration.MailSettingsSectionGroup settings = (System.Net.Configuration.MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");
SmtpClient client = new SmtpClient(settings.Smtp.Network.Host);
client.Credentials = new NetworkCredential(settings.Smtp.Network.UserName, settings.Smtp.Network.Password);
client.EnableSsl = true;
client.Send(nM);
}
and this is my web config code...
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="xyz#gmail.com" >
<network host="smtp.gmail.com"
defaultCredentials="false" userName="xyz#gmail.com" password="xyz"
port="587"/>
</smtp>
</mailSettings>
</system.net>
this code is working fine but how i will change the sender display name,normally we use like this
mM.From = new MailAddress("xyz#gmail.com","xyz");
but in my case I am not writing sender mail address any where , I am just getting from web config,then how I will change that name..i have current login user in my session,I want to display that name as a sender...
should put it on smtp from tag
<smtp deliveryMethod="Network" from="XYZ<xyz#gmail.com>">
<network host="smtp.gmail.com"
defaultCredentials="false" userName="xyz#gmail.com" password="xyz"
port="587"/>
You could set it like this in you web.config:
<network host="smtp.gmail.com"
defaultCredentials="false"
userName=""xyz"<xyz#gmail.com>"
password="xyz"
port="587"/>
Refer the supported formats for MailAddress.
How to get the "from" attribute value from the asp.net SMTP client configuration in the web.config file?
<mailSettings>
<smtp deliveryMethod="Network" from="administrator#somewebsite.com">
<network host="somehosting" userName="someusername" password="somepassword"/>
</smtp>
</mailSettings>
Like this:
var mailSettings = (MailSettingsSectionGroup)WebConfigurationManager.GetSection("system.net/mailSettings");
string from = mailSettings.From;
Another approach is to use named sections:
Application config:
<configuration>
<configSections>
<sectionGroup name="mailSettings">
<section name="DefaultSmtpProvider" type="System.Net.Configuration.SmtpSection"/>
</sectionGroup>
</configSections>
<mailSettings>
<DefaultSmtpProvider from="YourAddress#YourDomain.com">
<network host="#host" userName="#userName" password="#password" defaultCredentials ="false" />
</DefaultSmtpProvider>
</mailSettings>
</configuration>
Init Code:
SmtpSection smtpSettings = (SmtpSection)ConfigurationManager.GetSection("mailSettings/DefaultSmtpProvider");
var message= new MailMessage(smtpSettings.From, recipientAddress};
This will also allow you to have multiple smtp settings within one config in case you ever need to switch smtpClients. I forgot to state, you will have to build up the smtp client manually if this approach is used:
new SmtpClient
{
DeliveryMethod = smtpSettings.DeliveryMethod,
Host = smtpSettings.Network.Host,
UseDefaultCredentials = smtpSettings.Network.DefaultCredentials,
Credentials = new NetworkCredential(smtpSettings.Network.UserName, smtpSettings.Network.Password)
};
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! :)
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!