Hello. Since my graphomania is progressing, I decided to add another article. The article will, as always, with examples in C #, but a more or less skillful VB "schnik will easily rewrite all the code under VB.NET.
Let's start, perhaps
For sending e-mail we need to use three objects. System.Net.Mail.SmtpClient to send a message, System.Net.Mail.MailMessage to represent a message, and System.Net.NetworkCredential to authorize.
Let's create a client:
Var client = new SmtpClient("$Mail-Server-Address$", $Mail-Server-Port$);
constructor reference:
SmtpClient(string host, int port);
Let's create an instance of the MailMessage class:
Var msg = new MailMessage("from", "to");
Now let's set the subject and content of the letter (by the way, everything can be set in the constructor). As well as the login and password for the smtp client. Then we will send an email using the Send method of our client object:
Var client = new SmtpClient("smtp.yandex.ru"); var msg = new MailMessage("from", "to"); msg.Subject = "My test message"; msg.Body = "Hello, my friend! Just imagine, that I just wrote a incredible program to send this meaningless letter to you a billion times only by several clicks!"; msg.SubjectEncoding = Encoding.UTF8; msg.BodyEncoding = Encoding.UTF8; // priority msg.Priority = MailPriority.High; // message body in html? msg.IsBodyHtml = false; client.Credentials = new NetworkCredential("login", "password"); client.Send(msg);
That's all. Be sure to enter all required information correctly.
Comments()
ruslang02 30
CoolHacker, can I borrow this code from you for part 3 of the web browser?
Coolhacker 770 ruslang02 30Thank you, I will use it to send links by e-mail
like in Firefox
Programmatically send email using various services
- .net,
Very often you have to deal with sending emails from program code. You don't have to look far for examples.
This article is a hint and does not reveal anything new, but before writing it, I looked similar on the Internet and was quite surprised that almost everywhere they offer either a non-working or outdated solution, or simply written illiterately.
The first thing you should not do with these examples is to use System.Web.Mail, which has been deprecated for a long time, and since visual studio 2010 the System.Web library can't even be added without knowing full path to the corresponding DLL.
Instead, it is proposed to use the System.Net library.
using System.Net; using System.Net.Mail;
So, the simplest, it is also the most important and often used is sending a letter from your mail server The on which the SMTP client is configured. As you understand, the server can be either the one on which the application is running, or a remote one, on which you have the rights to send letters without additional authorization.
Sample code for sending email from local machine:
Using mail services such as Gmail, Yandex, Mail.ru, etc. everything is the same, only parameters with authorization are added.
SMTP server: smtp.gmail.com
Port: 587
using (MailMessage mm = new MailMessage("Name
SMTP server: smtp.yandex.ru
Port: 25
using (MailMessage mm = new MailMessage("Name
SMTP server: smtp.mail.ru
Port: 25
using (MailMessage mm = new MailMessage("Name
If you have Mailbox on the mail.ru service ends with inbox.ru, list.ru or bk.ru, then the address of the SMTP server (smtp.inbox.ru, smtp.list.ru and smtp.bk.ru) changes accordingly.
As you can see, in order to use any other mail service in your programs, you only need to find out the SMTP server address and port, as well as the authorization rules.
It must also be remembered that almost all third-party email services impose limits on the number of emails sent in a period of time.
Tags: email, sending emails, smtp
One of the most requested functions on the site is the application or order form, the data from which is sent to the mail of the site owner. As a rule, such forms are simple and consist of two or three fields for data entry. How to create such an order form? This will require the use of the HTML markup language and the language PHP programming.
The HTML markup language itself is simple, you just need to figure out how and where to put certain tags. With the PHP programming language, things are a bit more complicated.
It is not difficult for a programmer to create such a form, but some actions may seem difficult for an HTML coder.
Creating an html submit form
The first line will be next
Now let's put it all together.
Now let's make the fields in the form mandatory. We have the following code:
Create a file that accepts data from an HTML form
This will be a file called send.php
In the file, at the first stage, you need to receive data from the post array. To do this, we create two variables:
$fio = $_POST["fio"];
$email = $_POST["email"];
Variable names in php are preceded by a $ sign, and a semicolon is placed at the end of each line. $_POST is an array to which the data from the form is passed. In the html form, the send method method="post" is specified. Thus, two variables are taken from html forms. For the purposes of protecting your site, you need to pass these variables through several filters - php functions.
The first function will convert all characters that the user will try to add to the form:
In this case, new variables in php are not created, but existing ones are used. What the filter will do is convert the character "<" в "<". Также он поступить с другими символами, встречающимися в html коде.
The second function decodes the url if the user tries to add it to the form.
$fio = urldecode($fio);
$email = urldecode($email);
With the third function, we will remove spaces from the beginning and end of the string, if any:
$fio = trim($fio);
$email = trim($email);
There are other functions that allow you to filter php variables. Their use depends on how much you are afraid that an attacker will try to add program code to this form of sending data to html mail.
Validate data passed from HTML form to PHP file
In order to check if this code works, if the data is being transferred, you can simply display it on the screen using the echo function:
echo $fio;
echo "
";
echo $email;
The second line here is needed in order to separate the output of php variables into different lines.
Sending the received data from the HTML form to the mail using PHP
To send data to the mail, you need to use the mail function in PHP.
mail("to which address to send", "the subject of the letter", "Message (body of the letter)","From: from which email the letter is sent \r\n");
For example, you need to send data to the email of the site owner or manager [email protected]
The subject of the email should be clear, and the message of the email should contain what the user has specified in the HTML form.
mail(" [email protected]", "Application from the site", "Name:".$fio.". Email: ".$email ,"From: [email protected]\r\n");
You need to add a condition that will check if the form was sent using PHP to the specified email address.
if (mail(" [email protected]", "Order from the site", "Name:".$fio.". Email: ".$email ,"From: [email protected]\r\n"))
{
echo "message sent successfully";
) else (
}
Thus, the program code of the send.php file, which will send the HTML form data to the mail, will look like this:
$fio = $_POST["fio"];
$email = $_POST["email"];
$fio = htmlspecialchars($fio);
$email = htmlspecialchars($email);
$fio = urldecode($fio);
$email = urldecode($email);
$fio = trim($fio);
$email = trim($email);
//echo $fio;
// echo "
";
//echo $email;
if (mail(" [email protected]", "Application from the site", "Name:".$fio.". Email: ".$email ,"From: [email protected]\r\n"))
( echo "message sent successfully";
) else (
echo "errors occurred while sending the message";
}?>
The three lines to check if data is being passed to the file are commented out. If necessary, they can be removed, since they were only needed for debugging.
Place HTML and PHP form submission code in one file
In the comments to this article, many people ask how to make both the HTML form and the PHP code for sending data to the mail be in one file, not two.
To implement this work, you need to place the HTML code of the form in the send.php file and add a condition that will check for the presence of variables in the POST array (this array is passed from the form). That is, if the variables in the array do not exist, then you need to show the user a form. Otherwise, you need to take the data from the array and send it to the addressee.
Let's see how to change PHP code in send.php file:
//check if variables exist in POST array
if(!isset($_POST["fio"]) and !isset($_POST["email"]))(
?> ) else (
//show the form
$fio = $_POST["fio"];
$email = $_POST["email"];
$fio = htmlspecialchars($fio);
$email = htmlspecialchars($email);
$fio = urldecode($fio);
$email = urldecode($email);
$fio = trim($fio);
$email = trim($email);
if (mail(" [email protected]", "Application from the site", "Name:".$fio.". Email: ".$email ,"From: [email protected]\r\n"))(
echo "Message sent successfully";
) else (
echo "Errors occurred while sending the message";
}
}
?>
We check the existence of a variable in the POST array with the PHP isset() function. An exclamation point before this function in a condition means negation. That is, if the variable does not exist, then we need to show our form. If I didn't put an exclamation mark, then the condition would literally mean - "if exists, then show the form". And this is wrong in our case. Naturally, you can rename it to index.php. If you rename the file, then do not forget to rename the file name and in the line