PDA

View Full Version : مشکل در پیوست کردن فایل



taherisoftware
پنج شنبه 29 تیر 1391, 14:09 عصر
سلام
من با این کد ایمیل می فرستم.می خوام یک فایل رو هم پیوست کنم و همراه ایمیل بفرستم.چجوری این کار رو بکنم؟؟؟
لطفا کد مورد نیاز رو بهش اضافه کنید.


MailMessage _mail = new MailMessage();

SmtpClient _smtp = new SmtpClient();

_smtp.Credentials = new System.Net.NetworkCredential("info@akhazir.ir", "2440160067");

_mail.From = new MailAddress(Sender);

_mail.To.Add(new MailAddress(Reciver));

_mail.Headers.Add("To", Reciver);

//_mail.ReplyTo = new MailAddress(Sender);

_mail.Headers.Add("From", Sender);

_mail.Headers.Add("MIME-Version", "1.0");

_mail.Headers.Add("Content-type", "text/html; charset=UTF-8");

_mail.BodyEncoding = System.Text.Encoding.UTF8;

_mail.Body = Body;

_mail.IsBodyHtml = true;

_mail.Subject = Subject;

_smtp.Host = "mail.akhazir.ir";

_smtp.Send(_mail);

return true;

fakhravari
پنج شنبه 29 تیر 1391, 15:36 عصر
public string pathImage;
public string ImageFileName;
public string UpPath;

public void uploadpic()
{
UpPath = Server.MapPath("Email_Atachment/");
if (!Directory.Exists(UpPath))
{
Directory.CreateDirectory(Server.MapPath("Email_Atachment/"));
}

pathImage = Server.MapPath("Email_Atachment/");
String[] validext = { ".rar", ".zip"};
string ext = System.IO.Path.GetExtension(FileUpload1.PostedFile .FileName);

if (Array.IndexOf(validext, ext.ToLower()) < 0)
{
return;
}
else
{
int SIZE = FileUpload1.PostedFile.ContentLength;
if (SIZE < 4200000)
{
ImageFileName = System.IO.Path.GetFileName(FileUpload1.FileName);
while (System.IO.File.Exists(pathImage + ImageFileName))
{
ImageFileName = "1" + ImageFileName;
}

FileUpload1.PostedFile.SaveAs(pathImage + ImageFileName);
}
else
{
Label1.Text = "فایل شما به دلیل داشتن حجمی بیش از 2 مگابایت دریافت نشد. ";
}
}
}


protected void Button1_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
try
{
uploadpic();

System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage("To@a-velayat.com", "info@a-velayat.com");
mail.Body = TextBox1.Text.Trim();
mail.Subject = TextBox2.Text.Trim();
mail.BodyEncoding = Encoding.UTF8;
mail.SubjectEncoding = Encoding.UTF8;
mail.IsBodyHtml = false;
mail.Attachments.Add(new Attachment(pathImage + ImageFileName));
SmtpClient c = new SmtpClient();
c.Host = "mail.a-velayat.com";
c.Send(mail);
Label1.Text = "ایمیل ارسال شد";

TextBox1.Text = ""; TextBox2.Text = "";
ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "CheckScript", "alert('پیام شما با موفقیت ارسال شد')", true);

}
catch
{
ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "CheckScript", "alert('اخطار در ایمیل ارسالی')", true);
Label1.Text = "اخطار در ایمیل ارسالی";
}
}
}