Sending email from a web host without a username and password

I wrote a C # email sending program that works fine. In addition, I have a PHP script to send emails that work great.

But my question is: Is it possible to send an email from C #, like in PHP, where you do not need to specify credentials, server, ports, etc.

I would like to use C # instead of PHP because I am building an ASP.Net web application.

This is my current C # code:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Net.Mail; using System.Net; namespace $rootnamespace$ { public partial class $safeitemname$ : Form { public $safeitemname$() { InitializeComponent(); } private void AttachB_Click(object sender, EventArgs e) { if (AttachDia.ShowDialog() == DialogResult.OK) { string AttachF1 = AttachDia.FileName.ToString(); AttachTB.Text = AttachF1; AttachPB.Visible = true; AttachIIB.Visible = true; AttachB.Visible = false; } } private void AttachIIB_Click(object sender, EventArgs e) { if (AttachDia.ShowDialog() == DialogResult.OK) { string AttachF1 = AttachDia.FileName.ToString(); AttachIITB.Text = AttachF1; AttachPB.Visible = true; } } private void SendB_Click(object sender, EventArgs e) { try { SmtpClient client = new SmtpClient(EmailSmtpAdresTB.Text); client.EnableSsl = true; client.Timeout = 20000; client.DeliveryMethod = SmtpDeliveryMethod.Network; client.UseDefaultCredentials = false; client.Credentials = new NetworkCredential(EmailUserNameTB.Text, EmailUserPasswordTB.Text); MailMessage Msg = new MailMessage(); Msg.To.Add(SendToTB.Text); Msg.From = new MailAddress(SendFromTB.Text); Msg.Subject = SubjectTB.Text; Msg.Body = EmailTB.Text; /// Add Attachments to mail or Not if (AttachTB.Text == "") { if (EmailSmtpPortTB.Text != null) client.Port = System.Convert.ToInt32(EmailSmtpPortTB.Text); client.Send(Msg); MessageBox.Show("Successfuly Send Message !"); } else { Msg.Attachments.Add(new Attachment(AttachTB.Text)); Msg.Attachments.Add(new Attachment(AttachIITB.Text)); if (EmailSmtpPortTB.Text != null) client.Port = System.Convert.ToInt32(EmailSmtpPortTB.Text); client.Send(Msg); MessageBox.Show("Successfuly Send Message !"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void settingsBindingNavigatorSaveItem_Click(object sender, EventArgs e) { this.Validate(); this.settingsBindingSource.EndEdit(); this.tableAdapterManager.UpdateAll(this.awDushiHomesDBDataSet); } private void awDushiHomesEmail_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the 'awDushiHomesDBDataSet.Settings' table. You can move, or remove it, as needed. this.settingsTableAdapter.Fill(this.awDushiHomesDBDataSet.Settings); } } } 

Here's how to do it in PHP:

 <?php //define the receiver of the email $to = ' test@hotmail.com '; //define the subject of the email $subject = 'Test email with attachment'; //create a boundary string. It must be unique //so we use the MD5 algorithm to generate a random hash $random_hash = md5(date('r', time())); //define the headers we want passed. Note that they are separated with \r\n $headers = "From: webmaster@example.com \r\nReply-To: webmaster@example.com "; //add boundary string and mime type specification $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; //read the atachment file contents into a string, //encode it with MIME base64, //and split it into smaller chunks $attachment = chunk_split(base64_encode(file_get_contents('PDFs\Doc1.pdf'))); //define the body of the message. ob_start(); //Turn on output buffering ?> --PHP-mixed-<?php echo $random_hash; ?> Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" --PHP-alt-<?php echo $random_hash; ?> Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Hello World!!! This is simple text email message. --PHP-alt-<?php echo $random_hash; ?> Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: 7bit <h2>Hello World!</h2> <p>This is something with <b>HTML</b> formatting.</p> --PHP-alt-<?php echo $random_hash; ?>-- --PHP-mixed-<?php echo $random_hash; ?> Content-Type: application/zip; name="Doc1.pdf" Content-Transfer-Encoding: base64 Content-Disposition: attachment <?php echo $attachment; ?> --PHP-mixed-<?php echo $random_hash; ?>-- <?php //copy current buffer contents into $message variable and delete current output buffer $message = ob_get_clean(); //send the email $mail_sent = @mail( $to, $subject, $message, $headers ); //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" echo $mail_sent ? "Mail sent" : "Mail failed"; ?> 

I am not asking you to write your code, but perhaps you could provide some information or let me know if this is possible or not.

Edit:

My question is not to not use the SMTP server, but how to send an email without entering a username and password. I know that it will only work if the request to send a message comes from my server.

+7
c # php email
source share
6 answers

The reason you can send mail in PHP without specifying SMTP credentials is because someone else has already configured php.ini or sendmail.ini for you (files that the php interpreter uses to get some values).

This usually happens with managed hosting (or if you use php on your computer with tools like AMPPS that can make you easily edit SMTP settings using the user interface and forget about it).

ASP.net/C# has an app.config or web.config file , where you can enter smtp settings (in <mailSettings> ) and, therefore, achieve the same result as PHP ( SmtpClient will automatically use credentials, stored there).

See the following questions for an example:

Config smtpClient and app.config system.net

SMTP Authentication Using MailSettings Configuration File

+11
source share

These messages, which are likely to be duplicated, you will find several ways to send email using ASP.NET.

  • How to send email in ASP.NET C #
  • Sending mail without installing an SMTP server

Note. . You cannot send emails without the smtp server, although you do not need one of yours if someone else allows you to use them.

+1
source share

If you have php installed on your server, you can simply send email through the main php function. You do not need credentials.

Built-in php email sending function

 mail($to,$subject,$message,$header); 

You can use this code for a detailed understanding.

 // recipients $to = " hello@hellosofts.com "; // note the comma // subject $subject = 'Testing Email'; // message $message = " Hello, I am sending email"; // To send HTML mail, the Content-type header must be set $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; // Additional headers $headers .= 'To: 'Zahir' <' zahir@hellosofts.com '>' . "\r\n"; $headers .= 'From: Admin | Hello Softs < do-not-reply@Hellosofts.com >' . "\r\n"; // Mail it mail($to, $subject, $message, $headers); 
+1
source share
 Host : localhost OS : Ubuntu 12.04 + Language : PHP 

Install sendmail on OS

 sudo apt-get install sendmail 

Create test-mail.php file
Enter the code inside:

 <?php $to = ' test_to@abc.com '; $subject = 'Test Mail'; $message = 'hello how are you ?'; $headers = 'From: test_from@abc.com ' . "\r\n" . 'Reply-To: test_from@abc.com ' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); if(mail($to, $subject, $message, $headers)){ echo "Mail send"; }else{ echo "Not send"; } echo "Now here"; ?> 

Mail will be sent to test_to@abc.com

Note : you do not need to write username / password.

+1
source share

You can configure the mail settings section in the web.config file in your web application, as shown below. If your site is hosted by a third-party hosting company, you can ask them for smtp details (smtp username, smtp password, server name or IP address and port number). If you have this configuration in web.config and smtp is enabled (on the server), you should be able to send emails without specifying credentials in C # code.

 <system.net> <mailSettings> <smtp from=" you@yoursite.com "> <network password="password01" userName="smtpUsername" host="smtp.server.com" port="25"/> </smtp> </mailSettings> </system.net> 
+1
source share

You can send mail without SMTP or without credentials: No ..

If you do not want to write your credentials in your php files (under development), you can use .env

https://github.com/vlucas/phpdotenv

Example .env file:

 DB_HOST='example' DB_PORT='example' DB_USER='postgres' DB_PASS='example' SMTP_HOST='example' SMTP_PORT='example' SMTP_USER='example' SMTP_PASS='example' HOST='example' 

then you can use these env variables in your php files:

 getenv('SMTP_USER') 

You can also glue the .env file or not share it. But this is not the most important security issue that anyone should care about. If someone breaks into your server, you are already having problems.

I am not a .NET MVC developer, but you can set environment variables (under development) too

In any case, you need to write them somewhere, or they are already written. And this is not a big security issue unless you post your code somewhere like github etc.

NOTE. If environment variables can cause production performance issues, especially with php

0
source share

All Articles