I am developing a windows application using C#, in which i want to send SMS to some user based on some condition. i goes through the many forum post to "Send SMS using SMTP Server" but none of them use-full for me. In this i got some clue to send SMS through Gmail SMTP but not working as i think it is carrier specific (not sure).
My code sample :
try
{
MailMessage message = new MailMessage();
message.To.Add("1568235685#sms.sancharnet.in");
message.From = new MailAddress("sameone#gmail.com"); //See the note afterwards...
message.Body = "Hi, How r you ?";
SmtpClient smtp = new SmtpClient("smtp.gmail.com");
smtp.EnableSsl = true;
smtp.Port = 587;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential("someonet#gmail.com", "password");
smtp.Send(message);
MessageBox.Show("Message sent successfully");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}
above code not giving any exception or error but also i am not getting any sms on my number as well.
So, what i want to ask that is there any way to send SMS using SMTP server to the mobile number of any carrier?
You have to send to the SMS gateway. It is provider specific.
Wikipedia has a List of SMS Gateways.
For example, to send to a Sprint PCS number you would send to number#messaging.sprintpcs.com, where number is the phone number (i.e. 5551234567, or whatever).
For those who have looked so much for a free way to send SMS from a web app, and are in France, and having FreeMobile as operator, I've just found a way in calling a free web service provided by FreeMobile. I've written this code in C# and it works fine.
private void SendSMSAlert(String message)
{
try
{
String url = "https://smsapi.free-mobile.fr/sendmsg?user="YourFreeMobileIdentifierHere"&pass="YOURPASSHERE"&msg=" + message;
var request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
response.Close();
}
catch(WebException e)
{
System.Diagnostics.Trace.WriteLine("SMS Not Sent! Exception "+e.ToString());
}
}
So if you have a freeMobile line in France, you can get your Pass from https://mobile.free.fr/moncompte/
Then, if you need to forward the SMS to other numbers, it can be done with many mobile apps on AppStore or GooglePlay.
I hope this helps!
Related
I would like to add email functionality to a WinForm program I'm writing in C#. I have an Android app that has email functionality. What it does is set up the email but then lets the user choose the email program, etc. Once that is chosen the email body is completed. But it's up to the use to select what email app they want to use.
I would like to do the same in Windows but I don't see how. I have tried the following (based on other questions and responses here) :
_from = new MailAddress("my email address", "xxxx");
_to = new MailAddress("xxxx3333#gmail.com", "yyyy");
SmtpClient smtp = new SmtpClient("smtp.gmail.com");
smtp.UseDefaultCredentials = true;
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp msgMail = new MailMessage();
smtp.Body = text;
msgMail.Subject = "Subject";
msgMail.From = _from;
msgMail.To.Add(_to);
smtp.EnableSsl = true;
msgMail.Subject = _subject;
msgMail.Body = Text;
msgMail.IsBodyHtml = false;
try
{
mailClient.Send(msgMail);
}
catch (Exception ex)
{
string msg = "Exception caught in sending the email: " + ex.ToString();
showMessage(msg);
}
msgMail.Dispose();
But I get:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
With similar code in Android, my program just gets to an email form but lets the user decide what email add they will use.
Is there a way to do this in Windows?
There is an almost identical question and response here:
C# Windows Form Application - Send email using gmail smtp
And I think I've followed this but...doesn't work.
To directly answer your question - you probably haven't enabled less secure apps on the gmail account you are using.
Otherwise though, you could investigate the syntax of mailto if you want to let the user elect a mail client to use to send the email: https://www.labnol.org/internet/email/learn-mailto-syntax/6748/
From the link:
Send an email to Barack Obama with the subject “Congrats Obama” and some text in the body of the email message
<a href=”mailto:obama#whitehouse.gov?
subject=Congrats%20Obama&body=Enjoy%20your%20stay%0ARegards%20″>
This isn't directly related to C#/Windows - but I do know entering mailto:someone#somewhere.com at the Run prompt works:
Presumably then you could do something like: (untested)
Process.Run("mailto:someone#somewhere.com");
From the server response messages it looks like you have to provide login credentials before you are allowed to send.
Replace:
smtp.UseDefaultCredentials = true;
With:
smtp.Credentials = new System.Net.NetworkCredential("yourusername", "yourpassword");
This should do the trick.
You may have forgotten in your code to add the Host
Try to use this :
SmtpClient smtp = new SmtpClient();
smtp.UseDefaultCredentials = true;
smtp.Host = "SRVMAIL";
I am trying to send an email to a fake email address and get a SmtpFailedRecipientException, however when I send the email to a fake address on my company domain, no exception is thrown. I am developing in VS2015 with .NET 4.5.2 and running the console application on a Windows 7 environment.
This code works correctly and sends emails when the email addresses are valid, and it will even throw a SmtpFailedRecipientException when I try sending to an invalid gmail or hotmail address. It seems that the only time an exception isn't thrown is when I try to send to a fake address within my company domain. Our company uses Office365 and I get an 'Undeliverable' message in my inbox when I try to send to the fake address from my address.
Does anyone have an idea why this is happening? I suspect it might be a setting on the mail server, but I dont have access to the mail server to check and see. I also might just be missing something obvious and be making a rookie mistake.
// Initialize SMTP client
SmtpClient client = null;
try
{
string from = "myEmailAddress#mycompany.com", to = "fakeEmailAddress#mycompany.com";
// Build client
client = new SmtpClient("mail.mycompany.com");
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
// Create message
using (MailMessage message = new MailMessage(from, to)
{
Subject = "Test email to fake address",
Body = "This email won't be sent"
})
{
// Send message
client.Send(message);
}
}
catch (SmtpFailedRecipientException ex)
{
Console.WriteLine($"SmtpFailedRecipientException caught.{Environment.NewLine}{ex.ToString()}");
}
catch (Exception ex)
{
Console.WriteLine($"Exception caught.{Environment.NewLine}{ex.ToString()}");
}
All,
I'm writing an application that will allow customers to submit support tickets directly from their desktop. That being said, I'd like the "FROM" email address to be their email address.
I currently have the following code:
public void SendTicketEmail()
{
try
{
string tEmail = materialListView1.SelectedItems[0].SubItems[1].Text;
string tPhone = materialListView1.SelectedItems[0].SubItems[2].Text;
string tUser = materialListView1.SelectedItems[0].Text;
MailMessage mail = new MailMessage(tEmail, "my email");
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "smtp.simplifymsp.com";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
client.Send(mail);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
I'm assuming that my resolution at this point is to request that my hosting service enable an option where I can allow smtp outgoing emails without authentication on a specified port?
The alternative here is to have all of the support emails from each customer come from one of my preset email addresses and include a Customer ID in the subject, then create a workflow in my helpdesk ticketing system for each customer that assigns the customer's information to that ticket. That's more work than I care to do, especially when scaling.
Thank you in advance.
For what it's worth, I resolved this issue rather simply. FreshService (the website that I use for my ticketing system) reacts well with the following code:
mail.From = new MailAddress("me#me.com", "customer#customer.com");
Even though the email is coming from "me#me.com," FreshService still reads the ticket as if it came from the customer. Works beautifully.
Thank you all for your time.
I am using local host to send bulk mails through SES. This question is answered by many but none of the solutions is helping me. The problem is I could send 100/ 150 mails at a time after that the above error is showing up. I tried to dispose of the client as suggested by some, but not working. I am using C# code to do this. Any answers/ suggestions is much appreciated. The below is the code I am using to send bulk mail using for loop. You might be thinking it might be a throttling issue, it is not because we have 70 emails/second and 500000 emails per day.
Parallel.For(0, mail.Count, i =>
{
// Replace with your "From" address. This address must be verified.
String TO = mail; // Replace with a "To" address. If your account is still in the
// sandbox, this address must be verified.
// Create an SMTP client with the specified host name and port.
using (System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(HOST, PORT))
{
// Create a network credential with your SMTP user name and password.
client.Credentials = new System.Net.NetworkCredential(SMTP_USERNAME, SMTP_PASSWORD);
//Use SSL when accessing Amazon SES. The SMTP session will begin on an unencrypted connection, and then
//the client will issue a STARTTLS command to upgrade to an encrypted connection using SSL.
client.EnableSsl = true;
System.Net.Mail.MailMessage message1 = new System.Net.Mail.MailMessage(FROM, TO, SUBJECT, BODY);
message1.IsBodyHtml = true;
client.Send(message1);
client.Dispose();
}
});
I don't know the exact reason why it is working now, but it's working. I changed the logic of the above code, it started working. Instead of fetching the SMTP connection each time, for sending each mail previously, this time I fetched the smtp connection only once and used it to send all the bulk mails at once and it started working.But the problem is the sending time, it is taking too much to send all the mails.Anyways I will find the solution for this also.
using (System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(HOST, PORT))
{
client.Credentials = new System.Net.NetworkCredential(SMTP_USERNAME, SMTP_PASSWORD);
client.EnableSsl = true;
for(i=0;i<mail.Count;i++)
{
String TO = mail[i];
System.Net.Mail.MailMessage message1 = new System.Net.Mail.MailMessage(FROM, TO, SUBJECT, BODY);
message1.IsBodyHtml = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Send(message1);
}
client.Dispose();
}
Label1.Text = mail.Count.ToString() + " mails sent !!";
This code works locally, but when I upload it to my server on Godaddy, it does not send the e-mail. Any idea why it doesn't work on their server? What do I need to change?
try {
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("Myemail#gmail.com");
mail.To.Add("Myemail#gmail.com");
mail.Subject = "New sign up";
mail.Body = "New member";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("Myemail#gmail.com", "**Mypass**");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
} catch(Exception ex) {
throw ex;
}
They may be blocking outgoing SMTP connections in order to prevent spammers from using their service to send spam. You should check what error messages you're getting and check your server host's policy.
There are a couple of things you need to do when sending from inside a site hosted from Godaddy. Use their relay server to send the message (this won't work from your dev machine, you'll have to test it live after you upload it). Here is the relay server info. Also make sure the "from" address is an email within the same domain. I usually use the same as the toAddress. See here for info on why this is necessary.
This is the code I'm using to send from a site inside Godaddy:
btnSend.Disabled = true;
const string serverHost = "relay-hosting.secureserver.net";
var msg = new MailMessage(toAddress, toAddress);
msg.ReplyTo = new MailAddress(emailFrom);
msg.Subject = subject;
msg.Body = emailBody;
msg.IsBodyHtml = false;
try
{
var smtp = new SmtpClient();
smtp.Host = serverHost;
smtp.Credentials = new System.Net.NetworkCredential("account", "password");
smtp.Send(msg);
}
catch (Exception e)
{
//Log the errors so that we can see them somewhere
}
You need to send your email via the godaddy smtp servers. I experienced the same issue with them before I think. I believe they give instructions of how to login via their FAQ.
If you have ssh access to the server, try to telnet smtp.google.com via 25 and 465 ports also. If you get a timeout, then you're likely firewalled from connecting to these ports outside a certain IP range.
Port 587 is for TLS. As you're using SSL, try port 465.