I created an ASP.NET website using C# that targets .NET 4.8 using Visual Studio.
The website is hosted at Ionos (aka 1&1) on a Windows Server, but I have no access to it or to the IIS console.
From my dev workstation, I can successfully send emails using MailSender.Send(....) define in the following code:
using System;
using System.Net;
using System.Net.Mail;
public class MailSender {
public static void Send(string server, int port, string body, string subject, string to = "xxxxxxx#gmail.com", string from = "noreply#xxxxxxxx.com") {
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
SmtpClient smtp = new SmtpClient(server, port);
smtp.Credentials = new NetworkCredential(user, pw);
smtp.EnableSsl = true;
MailMessage Msg = new MailMessage();
Msg.From = new MailAddress(from);
Msg.To.Add(to);
Msg.Subject = subject;
Msg.IsBodyHtml = true;
Msg.Body = BuildBody(body);
smtp.Send(Msg);
}
}
Nevertheless, when the exact same website is put on the host server, this fails with following error:
System.Net.Mail.SmtpException: Failure sending mail. ---> **System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed**.
at System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 offset, Int32 read, Boolean readLine)
at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine)
at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller)
at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)
at System.Net.Mail.SmtpClient.GetConnection()
at System.Net.Mail.SmtpClient.Send(MailMessage message) --- End of inner exception stack trace ---
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at MailSender.Send_New(String server, Int32 port, String body, String subject, String to, String from)
Could anyone give me a hint on what I can do to fix this?
What I tried, so far:
Port I use is now 587, but 465 or 25 give the same result.
I tried several SecurityProtocolType values, with no difference.
I tried many different ways of sending (System.Web.Mail, built-in WebMail class, etc...) with same result.
I also checked that my website actually targets .NET 4.8
Searching, I found many articles on Google and dev forums, with many tips, but nothing seems to help in my very case
I also contacted Ionos support, who seem to say they have no problem and that this is a dev problem, so they cannot help...
Now I definitely need help, because it's now more than 2 weeks I'm trying things with no succes :(
Thanks in advance for any help!!
Related
I started learning C# recently, and found a practical application that isn't too difficult to accomplish, but I need some help figuring out my link I'm pretty sure. Let me show you.
using System;
using System.Net;
using System.Text;
... Namespace class etc etc
Console.WriteLine("Trying...");
WebClient client = new WebClient();
var nameValue = new System.Collections.Specialized.NameValueCollection();
nameValue.Add("FIRST NAME", "myname");
nameValue.Add("LAST NAME", "mylname");
nameValue.Add("GRADE", "mygrade");
Uri uri = new Uri("https://docs.google.com/forms/d/e/1FAIpQLScB206NtDOPl4_lQfCk9SZwqKfVRARVm0dSLujSj4qkWulp3Q/viewform");
byte[] response = client.UploadValues(uri, "POST", nameValue);
string result = Encoding.UTF8.GetString(response);
The linked form is one I created to model my schools. It's an attendance form (HS senior!) and I'm just trying to get it from my email and auto fill it every morning (lol), but I'm focused on this section first. My code generates a 405:
Unhandled exception. System.Net.WebException: The remote server returned an error: (405) Method Not Allowed.
at System.Net.HttpWebRequest.GetResponse()
at System.Net.WebClient.GetWebResponse(WebRequest request)
at System.Net.WebClient.DownloadBits(WebRequest request, Stream writeStream)
at System.Net.WebClient.UploadBits(WebRequest request, Stream readStream, Byte[] buffer, Int32 chunkSize, Byte[] header, Byte[] footer)
at System.Net.WebClient.UploadValues(Uri address, String method, NameValueCollection data)
at AttendanceAutomaticac.Program.Main(String[] args) in C:\Users\Lee\source\repos\AttendanceAutomaticac\Program.cs:line 18
Edit: Can I use any apis for this that someone knows of?
Bad link probably, but it works? I found this code via a link to google on a forum, and modified it slightly to fit my form. I'm also not quite sure whether or not the 3rd value will work for a single choice answer... So, I just need any other solution / explanation, I'm not too great at debugging and problem solving errors beyond StackOverflow in C# yet as I have minimal experience with it.
i'm current deploy one application into docker container, this app have function sending mail
(base on c# netcore 2.2)
but i can not sending mail (while mean i can sending mail from localhost)
the message of exception in docker
System.ComponentModel.Win32Exception: GSSAPI operation failed with
error - Unspecified GSS failure. Minor code may provide more
information (Cannot find KDC for realm "xxx.yyyy"). at
System.Net.Security.NegotiateStreamPal.AcquireCredentialsHandle(String
package, Boolean isServer, NetworkCredential credential) at
System.Net.NTAuthentication.Initialize(Boolean isServer, String
package, NetworkCredential credential, String spn, ContextFlagsPal
requestedContextFlags, ChannelBinding channelBinding) at
System.Net.Mail.SmtpNegotiateAuthenticationModule.Authenticate(String
challenge, NetworkCredential credential, Object sessionCookie, String
spn, ChannelBinding channelBindingToken) at
System.Net.Mail.SmtpConnection.SetContextAndTryAuthenticate(ISmtpAuthenticationModule
module, NetworkCredential credential, ContextAwareResult context)
at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32
port) at System.Net.Mail.SmtpTransport.GetConnection(String host,
Int32 port) at System.Net.Mail.SmtpClient.GetConnection() at
System.Net.Mail.SmtpClient.Send(MailMessage message) --- End of
inner exception stack trace --- at
System.Net.Mail.SmtpClient.Send(MailMessage message)
i have try some help from google but can not, so please help me
thanks
It seems there is an issue in dotnet on linux, when you send mails with the GSSAPI authentication type:
https://github.com/dotnet/runtime/issues/2174, https://github.com/dotnet/runtime/issues/25885, https://github.com/dotnet/runtime/issues/27285
Try installing "gss-ntlmssp" in the container if you need to use GSSAPI.
Or, if your server accepts other authentication types, use another one, like NTLM:
var cred = new NetworkCredential(user, password, domain);
var cache = new CredentialCache();
cache.Add(host, 25, "NTLM", cred);
client.Credentials = cache;
In our MVC 5 site we have a VerifyEmail method that contacts a service to check that an email exists. We do the basic preliminary checks, to make sure it looks like a valid email, etc, then we pass it to the service.
We're using a WebClient.DownloadString() function. When we navigate to the appropriate View and trigger the method, we get these errors:
When debugging:
<h2>Bad Request - Invalid Hostname</h2>
<hr><p>HTTP Error 400. The request hostname is invalid.</p>
When we're not debugging, we get this:
System.Net.WebException: Unable to connect to the remote server --->
System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:63595
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
--- End of inner exception stack trace ---
If I call the method (VerifyEmailOrgEmailVerificationClient.VerifyEmail) from LinqPad it works!!!
I get this:
MX record about emailserver.com exists.<br/>
Connection succeeded to emailserver-com.mail.protection.outlook.com SMTP.<br/>
220 SN1NAM01FT022.mail.protection.outlook.com Microsoft ESMTP MAIL Service ready at Mon, 29 Feb 2016 21:08:50 +0000<br/>
> HELO verify-email.org<br/>
250 SN1NAM01FT022.mail.protection.outlook.com Hello [verify-email.org]<br/>
> MAIL FROM: <check#verify-email.org><br/>
=250 2.1.0 Sender OK<br/>
> RCPT TO: <redacted#emailserver.com><br/>
=250 2.1.5 Recipient OK<br/>
That's what we should get. So, the code works from LinqPad, but not when we call it as a website. We put a test method together and get the same results. With our test, we're just trying to get a response from WebClient.DownloadString(). Even like that we get an error.
Here's our test method:
using System;
using System.IO;
using System.Net;
using System.Web.Mvc;
using PublicationSystem.ViewModels;
namespace TestWebClient.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
var newModel = new TestViewModel();
using (var webClient = new WebClient())
{
webClient.Encoding = System.Text.Encoding.UTF8;
webClient.Headers["Content-Type"] = "application/json;charset=UTF-8";
var requestUrl = string.Empty;// GetRequestUrl(email);
try
{
requestUrl =
"http://api.verify-email.org/api.php?usr=igiglobal&pwd=emailsareweak&check=mkenyon#mkptechnologies.com";
newModel.ResponseString = webClient.DownloadString(requestUrl);
}
catch (WebException exception)
{
string responseText;
if (exception.Response != null)
{
using (var reader = new StreamReader(exception.Response.GetResponseStream()))
{
responseText = reader.ReadToEnd();
}
newModel.ResponseString = responseText;
}
else
{
newModel.ResponseString = exception.ToString();
}
}
catch (Exception exception)
{
newModel.ResponseString = exception.ToString();
}
}
return View(newModel);
}
Our AppPool is set for .Net 4.0. The app compiles for .Net 4.5. We're using MVC 5.
Any idea why our method, specifically the WebClient.DownloadString() fails while we're running the site, but works if we call the code from LinqPad?
UPDATE: I created a new MVC project with Visual Studio 2013, just like this project. In the new test project, I pasted the same code, and have tried to get the references to match as exactly as possible. My code works in the test project. So something with the way my project is set up is blocking WebClient.DownloadString(). Using Fiddler, it looks like it doesn't even send the request out.
Is there a setting that could block WebClient.DownloadString() while testing an MVC site?
UPDATE 2: In the web.config I found this. Could this be it?
<system.net>
<defaultProxy>
<proxy proxyaddress="http://localhost:63595/" />
</defaultProxy>
</system.net>
In the web.config I found this. Commenting it out let WebClient.DownloadString() work again. Hope this helps somebody. I'll have to do more research on this.
<system.net>
<defaultProxy>
<proxy proxyaddress="http://localhost:63595/" />
</defaultProxy>
</system.net>
I'm trying to convert some Python code for use in a .Net website. The code retrieves a message stored in RFC822 format and sends the message to an SMTP server again using:
mail from: blah blah
rcpt to: blah blah
data
<send the text from the RFC822 file here>
.
So no parsing of the RFC822 file is required (fortunately!). In Python this is simply:
smtp = smtplib.SMTP(_SMTPSERVER)
smtp.sendmail(_FROMADDR, recipient, msg)
where the file has been read into the variable msg. Is there an easy way to do this in C#?
The built in C# SMTP objects don't offer a way to do this, or at least I haven't found a way. They seem to be based on the principle of building up a MailMessage by providing the addresses, subject and body separately. SmtpClient has a Send(string, string, string, string) method, but again this requires a separate subject and body so I guess it constructs the RFC822 formatted message for you.
If necessary I can write my own class to send the mail. It's such a simple requirement that it wouldn't take long. However if there is a way using the standard libraries they're probably less buggy than my code.
I would recommend using MimeKit to parse the message file and then use MailKit to send via SMTP. MailKit is based on MimeKit, so they work well together and MailKit's SmtpClient is superior to System.Net.Mail's implementation.
Parsing a message is as simple as this:
var message = MimeMessage.Load (fileName);
Sending the message is as simple as these few lines:
using (var client = new SmtpClient ()) {
client.Connect ("smtp.gmail.com", 465, true);
client.Authenticate ("username", "password");
client.Send (message);
client.Disconnect (true);
}
You're right, the inbuilt stuff doesn't offer a solution for this.
My advice would be to simply write a bit of code that uses TcpClient and StreamReader / StreamWriter to interact with the SMTP server. It shouldn't need more than 50 lines of code.
This is easy to do, you need only to install MailKit and MimeKit from nuget.
Pay attention because if you don't need authentication, setting "useSsl" to false is not enough, it doesn't work. You need to set MailKit.Security.SecureSocketOptions.None
var message = MimeMessage.Load("pathToEml");
using (var client = new MailKit.Net.Smtp.SmtpClient())
{
client.Connect("smtp.yourserver.yourdomain", 25, MailKit.Security.SecureSocketOptions.None); //set last param to true for authentication
//client.Authenticate("username", "password");
client.Send(message);
client.Disconnect(true);
}
I have recently purchased a new computer, and now my e-mails never get sent, and there are NEVER any exceptions thrown or anything.
Can somebody please provide some samples that work using the SmtpClient class? Any help at all will be greatly appreciated.
Thank you
Updates
Ok - I have added credentials now. And can SUCCESSFULLY SEND e-mail synchronously. But I can still not send them asynchronously.
Old:
After trying to send e-mail synchronously, I receive the following exception:
Transaction failed. The server response was:
5.7.1 <myfriend#hotmails.com>: Relay access denied.
You can send mail through Async(). How means you should follow the below code,
smtpClient.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted);
smtpClient.SendAsync(mailMessage, mailMessage);
and, if you are using async, you need to also have the event handler like,
static void smtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
//to be implemented
}
By using this, you can send Mail.
You could first try the synchronous Send method to verify that everything is setup correctly with the SMTP server and that you don't get any exceptions:
var client = new SmtpClient("smtp.somehost.com");
var message = new MailMessage();
message.From = new MailAddress("from#somehost.com");
message.To.Add(new MailAddress("to#somehost.com"));
message.Subject = "test";
message.Body = "test body";
client.Send(message);
Remark: In .NET 4 SmtpClient implements IDisposable so make sure you wrap it in a using statement.