Tôi đã viết một chương trình C# để gửi email, hoạt động hoàn hảo. Ngoài ra, tôi có một kịch bản PHP để gửi email, hoạt động hoàn hảo.Gửi email từ webhosting mà không cần phải sử dụng tên người dùng và mật khẩu
Nhưng câu hỏi của tôi là: Có thể gửi một email với C# như bạn làm từ PHP mà bạn không cần phải xác định thông tin, máy chủ, cổng, vv
Tôi muốn sử dụng C# thay vì PHP, bởi vì tôi đang tạo một ứng dụng web ASP.Net.
Đây là hiện mã của tôi C#:
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);
}
}
}
Đây là cách nó được thực hiện trong PHP:
<?php
//define the receiver of the email
$to = '[email protected]';
//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: [email protected]\r\nReply-To: [email protected]";
//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";
?>
Tôi không yêu cầu để bạn có thể viết mã của tôi, nhưng có lẽ bạn có thể cung cấp một số thông tin hoặc cho tôi biết nếu nó có thể hay không.
Edit:
Câu hỏi của tôi không được về việc không sử dụng một máy chủ smtp nhưng làm thế nào để gửi một email mà không cần phải nhập tên tài khoản và mật khẩu. Tôi biết, nó sẽ chỉ hoạt động nếu yêu cầu gửi email đến từ máy chủ của tôi.
Trên thực tế PHP * không * sử dụng [cài đặt khác nhau mail server] (http: // php. net/manual/en/mail.configuration.php), tuy nhiên chúng được lưu trữ trong tệp php.ini và có các giá trị mặc định dựng sẵn. Tôi cho rằng bạn có thể thay đổi chúng trong thời gian chạy với 'ini_set()' nếu bạn cần thực hiện các thay đổi không phải là toàn hệ thống. – Mike
@ Giống như tôi đã không thực sự biết những gì để tìm nhưng tôi googled rất nhiều, có thể cho những điều sai trái ... tôi sẽ thử các liên kết bạn đã cho tôi. client.UseDefaultCredentials = true; trông đầy hứa hẹn. – Creator
Thực ra tôi đã xóa nhận xét cuối cùng của mình bởi vì tôi không nghĩ rằng điều đó tôi đã làm. Nó xuất hiện được sử dụng khi bạn cần xác thực với máy chủ thư. Đúng nếu tôi sai. Tôi không biết C# chút nào. – Mike